Q:

Using if with Scanner in Java isn't working properly when comparing strings

import java.util.*;

class Sept1Little {
    public static void main (String args []) {

        Scanner s = new Scanner(System.in);

        System.out.println("Hey! I'm not gonna program today o.o but I'm gonna do a little program :3");
        System.out.println("Let's play with some math!");
        System.out.println("Enter two numbers and magic will happen");
        System.out.print("Enter the first number: ");
        float a = s.nextInt();
        System.out.print("Enter the second number: ");
        float b = s.nextFloat();
        float c = (float) Math.pow( a, b);
        System.out.printf("The number is :%f\n" , c);
        System.out.println("We love Bernie Sanders!");
        System.out.println("Come on, let's push him over the line!");
        int poll1 = 30;
        System.out.println("Current poll amount: " + poll1);
        System.out.println("Let's add 7% and an extra 1%!");
        int poll2 = poll1 + 7;
        int poll3 = ++poll2;
        System.out.println("Bernie Sanders would get :" + poll3 + ". Bernie would get a better percentage than Hillo Clinto-Money");
        System.out.println("Write in the best US President Nominee: ");
        String president = Keyboard.readString();
        if (president.equals("Bernie Sanders")){
            System.out.println("You chose Bernie Sanders.");

        }
        else if (president.equals("Donald Trump")) {
            System.out.println("You chose Donald Trump");

        }
        else if (president.equals("Hillary Clinton")) {
            System.out.println("You chose Hillary Clinton ");

        }
        else {
            System.out.println("Choose someone we know...");

        }
        System.out.println("Let's calculate how much money you owe the govt by... SQUARE ROOTING IT!");
        System.out.print("Enter the amount of money you owe the govt: ");
        float debt1 = Keyboard.readFloat();
        System.out.printf("The amount is now :%f\n" , Math.sqrt(debt1));
        System.out.println("Welcome to the Euro to Dollar Convertor!");
        System.out.print("Enter the amount in US Dollars you wish to convert to Euro: ");
        float euro1 = Keyboard.readFloat();
        final float rate = (float) 1.13;
        System.out.printf("The amount in Euro is %f\n" , euro1 / rate);
    }
}

As you can see, I am using Keyboard.readString(); when I ask for the nominee. When I use s.readLine();, it doesn't work, it skips to the else clause and then it crashes since it takes the president input for the float value afterwards. I'm sorry that it is a bit political but I didn't bother fiddling with it. When I put "Bernie Sanders" as "BernieSanders", I don't get the error, anyone knows why?

A:

I guess that it is because s.nextFloat(); and s.nextInt(); only reads the float and the int, respectively, not the full line so the cursor of the Scanner it's after the float or the int and not in the next line. Here an example:
If your input it's:
5 Bench
Prove

And you have in your code:
a= s.nextInt(); //5
b= s.nextLine(); //" Bench"

Why it isn't catching the Prove value? Because after using s.nextInt() the cursor of the Scanner it's after the 5 and not in the next line (the line in which we have Prove). To avoid it you have to read the rest of the line (useless for you because you don't want it) but without getting this value. Like this:
a= s.nextInt(); //5
s.nextLine(); //We go to the next line
b= s.nextLine(); //"Prove"

P.S.: You have to use s.nextLine(); each time you use a s.nextInt(); or s.nextFloat(); as the example that I put above.
I expect it will be helpful for you!

