Learning input from user



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?");
	System.out.println("What is 6 multiplied by 7?");
	System.out.println("Enter a number between 0.00 and 0.02.");
	System.out.println("Is there anything else you'd like?");

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?
What is 6 multiplied by 7?
Enter a number between 0.00 and 0.02.
Is there anything else you'd like?
No your majesty.


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)


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:

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

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


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


Yes sir, Exactly so!


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?


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


Yes i forgot to add that detail :disappointed: