0

My problems starts when I want to see the condition and then move to another line but there is no "goto" in java so I have no clue how to move from inside of this if and go to the next line one ther hand when I put return after that it doesn't run the code .... I would aprieciate I f you guys help me with this problem... source code is here :::

  //package CaloriesCalculator
 import java.util.Scanner;

class Main {
// @SuppressWarnings("null")
@SuppressWarnings("resource")
public static void main(String[] args) {
    // Initiate a new Scanner
    // here I have tried to use scanner
    boolean run = true;
    while(run){
    @SuppressWarnings("resource")
    Scanner userInputScanner = new Scanner(System.in);


    System.out.print("\nIf you are Male Press 1, If you are Female Press 2 ");

            int G = userInputScanner.nextInt();

            if(G == 1)

                {
                            System.out.print("Please Enter your Weight.."); 
                            int W = userInputScanner.nextInt();

                            if((W <300) && (W > 1)) {//error message


                                //      my problems start here !! I need to have the next input here but it doesn't work



                            }
                            System.out.print("Please Enter your Weight.."); 
                            W= userInputScanner.nextInt();
                            return;
                            //  int m;
                            System.out.print("Please Enter your Height");
                            int H = userInputScanner.nextInt();
                                System.out.print("Please Enter your Age.."); 
                                int A = userInputScanner.nextInt();

                                double BMR = 10*W + 6.25*H - 5*A +5 ;

                                    if( BMR > 0)    {System.out.print(" Your body needs the amount of calories : "+ BMR );}

                                        else { System.out.print("Do you know you are a mess ?  " );}





                            //int H = userInputScanner.nextInt();



                            //  else {System.out.print("Please Enter valid weight");}   
                }

            else if(G == 2)

                {

                        System.out.print("Please Enter your Weight.."); 
                        int W = userInputScanner.nextInt();

                        System.out.print("Please Enter your Height"); 
                        int H = userInputScanner.nextInt();

                        System.out.print("Please Enter your Age.."); 
                        int A = userInputScanner.nextInt();

                        double BMR = 10*W + 6.25*H - 5*A -161 ;


                                if(BMR > 0)
                                    {
                                        System.out.print(" Your body needs the amount of calories : "+ BMR );
                                    }

                                else
                                    {
                                        System.out.print("Do you know you are a mess ?  " );

                                    }


        }
            else
            {
                System.out.print(" Please put the right number or don't use program " );
            }
    }

}

}

Hamed Fazilat
  • 33
  • 1
  • 1
  • 5

1 Answers1

1

The nextInt() method doesn't read the whole line (with carriage return and line feed), thus when you reread the nextInt() you get an empty string, that's why your if doesn't execute the code in it.

You have to:

  1. First use nextLine() to read the entire line.

  2. Use Integer.parseInt() method to validate the integer input.

In other words:

Scanner userInputScanner = new Scanner(System.in);
String s = userInputScanner.nextLine();

try{
    Integer.parseInt(s);
}
catch(NumberFormatException ex){
    System.out.println("Its not a valid Integer");
}

Otherwise you can do a nextInt() and then a nextLine() to consume the \n:

int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
Community
  • 1
  • 1
Narmer
  • 1,414
  • 7
  • 16
  • tnx for the help but i couldn't make it work.. I am too beginner to JAVa to understand this code ... I couldn't find the right place for it or even if I did i got many other errors... :( – Hamed Fazilat Oct 22 '14 at 08:33
  • That's a pity! Try your exact same code, just add the line `userInputScanner.nextLine()` after `int G = userInputScanner.nextInt();` and `int W = userInputScanner.nextInt();` (or everytime you are using `nextInt()`) – Narmer Oct 22 '14 at 08:37