0

I'm trying to call a static method (printABC()) in this class but it's not working.

If I uncomment both of the lines marked T_T (1 and 2), it works! Why does it fail with only one of the lines?

import java.util.Scanner;
class pro0009 {
 static Scanner in = new Scanner(System.in);
 static int A,B,C;
 static void printABC(){
 String ABC = in.nextLine(); 
 ABC=ABC.replace("A"," "+A+" ");
 ABC=ABC.replace("B"," "+B+" ");
 ABC=ABC.replace("C"," "+C+" ");
 //System.out.print(ABC.substring(1));
 System.out.print(ABC);
 }
 public static void main(String[] args){
 int x = in.nextInt(); //1
 int y = in.nextInt(); //2
 int z = in.nextInt(); //3
 if(x<y){//1<2
 if(x<z){ //1<3
 if(y<z){//x<y<z 2<3
 //1<2<3
 A=x;
 B=y;
 C=z;
 printABC();//T_T 1
 System.out.println("Here");
 //pro0009.printABC();//T_T 2
 //System.out.println("Here2");
 }else{ //x<z<y
 A=x;
 B=z;
 C=y;
 }
 }else{//z<x<y
 A=z;
 B=x;
 C=y;
 }
 }else{//y<x
 if(y<z){
 if(x<z){//y<x<z
 A=y;
 B=x;
 C=z;
 }else{//y<z<x
 A=y;
 B=z;
 C=x;
 }
 }else{//z<y<x
 A=z;
 B=y;
 C=x;
 }
 }
 }
}
Shog9
160k36 gold badges237 silver badges242 bronze badges
asked Dec 31, 2010 at 19:36
6
  • 3
    When I see such a formulated question, something wants to cry deep inside of me:( Commented Dec 31, 2010 at 19:42
  • 1
    What do you mean by "it doesn't work"? What do you expect it to do, and what does it actually do? There seems to be a lot of code there that is not relevant. Can you reduce it to just the important parts? Commented Dec 31, 2010 at 19:46
  • 1
    @Smile: how does it fail? You're not providing us with any useful information here! What output are you expecting, what are you getting instead, what are your inputs... Commented Dec 31, 2010 at 19:46
  • thank! i try to input 1 2 3 but printABC() not called in my screen cursor skip to "Here" Commented Dec 31, 2010 at 19:49
  • printABC calls in.nextLine - what input do you give it? The first 1 2 3 will be assigned to A, B and C, but what do you enter later? Commented Dec 31, 2010 at 19:52

3 Answers 3

1

T_T 1 consumes the line entered. There's nothing for the in.nextLine() to consume in the buffer at T_T 2, so it's waiting for input.

answered Dec 31, 2010 at 19:54
Sign up to request clarification or add additional context in comments.

Comments

0

since you are calling a static method within a another static method, this should not cause a problem. The problem should be with the logic of the program. Please provide more details regarding teh functionality you are trying to implement.

answered Jan 3, 2011 at 9:51

Comments

0

The three nextInts in the beginning of the program don't eat the line terminator. First time nextLine is called, it reads the input upto this terminator and returns empty string. So the first printABC prints empty string as well. When nextLine is called for the second time, it reads the next line in the input which is what you expect.

To fix this you can simply call nextLine twice in printABC, ignoring the result of the first call, since it should always be empty.

answered Jan 3, 2011 at 10:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.