Learning input from user

java

#1

Hey guys, big question to me small to yal. Take the code I have
under me for example,

import java.util.Scanner;

public class JustTesting {
        public static void main(String[] args) {
	Scanner keyboard = new  Scanner(System.in);
	
	System.out.println("What city is the capital of France?");
	keyboard.next();
	
	System.out.println("What is 6 multiplied by 7?");
	keyboard.nextInt();
	
	System.out.println("Enter a number between 0.00 and 0.02.");
	keyboard.nextDouble();
	
	System.out.println("Is there anything else you'd like?");
	keyboard.next();
}
}

Ok my question is on the System.out.println("Is there anything else you'd like?");
I could type for example, "no thank you" and the program ends because there is nothing
more I understand that. But on the first System.out.println("What is the capital of France");
I can type "Paris" and it continues to run the rest of my code, well say for instance i wanted
to type "i dont know" well when I do that this is the error I get and I dont understand it because
I thought the .next(); function packaged whatever as a string.

When I type more than 1 word on first question I get this error:

What is the capital of France?
i dont know
Exception in thread "main" java.util.InputMismatchExcepti
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Forgetful.main(Forgetful.java:11)

I only get that error if I type more than 1 word on my first input.
But it doesn't give me the error for typing more than 1 word on my last input

This is what the code does if I only type 1 word on first question. Hope this helps.

What city is the capital of France?
Paris
What is 6 multiplied by 7?
42
Enter a number between 0.00 and 0.02.
0.01
Is there anything else you'd like?
No your majesty.


#2

Some :bug: From codecademy. I learned this concept from school , and use it all the time. So this is the concept. (DISCLAMER: My way is diffrent but is widely used)

So..

if you want to set equal what the user inputed from the System.out.println(); this is how you do it

First i set a public scanner so i can use it in any other methods i create inside the class:

public static Scanner in = new Scanner(System.in);

Then i do :

System.out.println("Enter number of cats");
int noCats = in.nextInt();

What this does is it stores what the user inputs for cats into an primitive type Int called noCats.

So, after that i could for example use an if statement with it:

if(noCats>8){
System.out.println("You have a ton of cats");
}
else if(noCats<1){
System.out.println("You should get a Cat!");
}
else{
System.out.print("The usual");
}

Hope you understood/helped! If you need some more clarifying be sure to ask! :smile:


#3

Hey @amanuel2 thanks for the quick reply & what you did
also stores the input from user into the noCats var correct?


#4

Yes sir, Exactly so!


#5

This is, in fact, not a bug. If you know how the Scanner works, then it throwing that error makes perfect sense. Let's see if I can explain:

Scanner is a powerful object that can be used to get input from many sources. As such, it does have quite a few methods which on first glance may seem to do what you want but don't actually do that. Scanner#next() simply gets the next parseable token (aka, one word) and accepts it. Then, your nextInt() call is actually getting a String, and so is your nextDouble() call. This will throw an InputMismatchException.

If you want to get a multi-word String from your input, you should use nextLine(), which will advance the Scanner past the current line (aka, until a new line is reached (\n)), and then return the input that was skipped as a String.

Does that make sense?


#6

Yeah @jacobsandersen thanks I had read some more info about the next()
and nextLine() last night but yes I understand it now thanks!


#7

Yes i forgot to add that detail :disappointed: