Smile.Hunter
Smile.Hunter

Reputation: 252

Static method not called

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;

         }
      }
   }

}

Upvotes: 0

Views: 1968

Answers (3)

adamax
adamax

Reputation: 3865

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.

Upvotes: 0

Programmer
Programmer

Reputation: 6753

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.

Upvotes: 0

RobertB
RobertB

Reputation: 4602

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.

Upvotes: 1

Related Questions