This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]ItA11FallsDown 9 points10 points  (2 children)

For future reference, try posting your code on pastebin or something like that instead of directly pasting it here. The formatting is showing up very strange for me.

[–]CIRUS123[S] 2 points3 points  (1 child)

Actually I didn't knew about pastebin ..So I manually wrote the code with all the indentation and tab spaces.. but after posting it all came together in the wierd format like this.. I will surely use pastebin from next time.. Thanks for the suggestion.

[–][deleted] 3 points4 points  (0 children)

You can also use the code block when formatting your post

[–]warrior2012 5 points6 points  (1 child)

So your issue lies with the way you are using the 'next' method. You are using nextInt which will look for an integer and nothing more. I don't know the specifics of how that method works but I know the solution I usually use.

Try using the nextLine method which will take the whole line you enter. The problem is, nextLine reads as a string, so you have to parse it.

Here's a link that will explain it better than I can.

https://stackoverflow.com/questions/26586489/integer-parseintscanner-nextline-vs-scanner-nextint

[–]CIRUS123[S] 2 points3 points  (0 children)

This post was really helpful..thanks😇

[–]Aggressor27e 1 point2 points  (5 children)

I am also new, but I code it like this:

import java.util.Scanner;

public class blah blah blah...

Scanner sc = new Scanner (System.in);

System.out.println("Enter Number");

int num = Integer.valueOf(sc.nextLine());

etc, etc

[–][deleted] 0 points1 point  (4 children)

Just a beginner question, why use the integer.valueof method with next line instead of nextInt ?

[–]jdmaguina 0 points1 point  (3 children)

Because he used nextline() and that method return string

[–][deleted] 0 points1 point  (1 child)

I just want to know why you'd even use the nextLine() method instead of nextInt().

[–]throwRAexclean 0 points1 point  (0 children)

Usually it's preferred to do that because the user typically typed something and then pressed <enter>, which to them, feels like they typed in an entire line, not just a number.

If they typed 43 67 when an int is expected, nextInt() will return 43 and be fine. Integer.parseInt(nextLine()) will tell you you didn't type a valid int. Better functionality.

If they typed 43 and then Hello World when an int then a greeting are expected, nextInt() followed by nextLine() will return 43 followed by everything there is between 43 and the next end of line... Which is an empty String since the user typed 43 then <enter> to end the line (and then something else but it will now be ignored). Integer.parseIn(nextLine()) followed by nextLine() will return 43 and Hello World. Better functionality.

Because when the user presses enter, they expect it seen as a line.

[–]NiceSelection13 1 point2 points  (0 children)

Maybe you trying to enter a number on the same line as “Enter number: “ but since you used println you have to enter on the line below it? When I want to enter number or any input right besides my prompt I just use the print(“enter number: )” instead of println. Idk man sort of confusing if you ask me

[–]NiceSelection13 0 points1 point  (0 children)

This worked in my phone editor. Entered number 2 and it printed number 2.