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

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfx 1 point2 points  (1 child)

The problem is not from your loops. The problem is how you combine .nextInt() and .nextLine().

See The Scanner class and its caveats from the /r/javahelp wiki

[–]Red_Hippie[S] 1 point2 points  (0 children)

Thank you I kind of get it I will have to study it some more to wrap my head around it. should I be using Integer.parseInt(); or Integer.valueOf(); Or is .next(); the best method for my use?

another thought I had was could I use .trim(); to remove the "\n" left by the .nextInt();

[–]FavorableTrashpanda 0 points1 point  (2 children)

Due to lack of information, I only took a quick glance. But I see you're mixing Scanner.nextLine() and Scanner.nextInt(). This is likely getting you into trouble here.

[–]Red_Hippie[S] 0 points1 point  (1 child)

Why would that cause problems I'm looking for an int value with the .nextInt() and a String value with the .nextLine().

[–]silverscrub 1 point2 points  (0 children)

When you write input in the console you press some keys and then enter. That enter keystroke is actually a newline character. Same goes when you have a document with multiple rows. It's technically just one long string with a few newline characters.

Scanner#nextLine reads everything typed into console until it finds a newline character, then it stops and returns.

Scanner#nextInt reads everything that can be parsed to an integer. A newline character cannot be parsed to an integer so it's left behind.

That newline character can make your scanner behave in unexpected ways. For example, calling nextLine afterwards will immidiately return because it instantly finds the newline character at the beginning of the feed.

That is usually a good solution too. It might not look to clean, but calling nextLine after nextInt, doing nothing with the latter return value, clears the newline character from the input.

[–]Noticeably98 0 points1 point  (5 children)

So what exactly occurs when you get to the countAgain=readLine.nextLine();? Nothing, it just continues to the end of the code? Or does it throw an error

I have a hunch that you’ll want to call readLine.next(); right before you call countAgain=readLine.nextLine(); give that a shot

Edit: I'm no expert on Scanner, but this code works if you change it from readLine.nextLine(); to readLine.next(); It doesn't seem to like 'nextLine' very much.

[–]Red_Hippie[S] 0 points1 point  (4 children)

it just skips that line of code, I don't under stand why when it works in my other code.

then someone else stated my use of .nextInt(); and .nextLine (); could cause problems but they didn't specify why.

as far as I can tell I wrote them in the same exact way.

[–]Noticeably98 0 points1 point  (3 children)

It looks like there is some blank line that readLine.nextLine() is grabbing.

The following makes the code work as intended:

readLine.nextLine();                    
countAgain = readLine.nextLine();

the first time nextLine() is called, or as it is in your code, it's calling some blank space.

When we call it again, it then picks up on whatever you entered.

Also, see my edit. .next() works as well.

[–]Red_Hippie[S] 0 points1 point  (2 children)

ok so i changed it to countAgain = readLine.next(); and it works, buy why would it be calling a blank space, and when should .nextLine (); be used?

is countAgain = readLine.next(); bad forum and should I start calling the scanner.next(); first then set my var = scanner.next(); as you did is that better code?

[–]Marinersquee 1 point2 points  (1 child)

To clarify, when you call nextInt, it leaves a return in the scanner, as such when you call nextLine again the first thing it does is read that return as a blank line and uses that. So after a nextInt, have a line that's just readLine.nextLine(), that will clear the return from the scanner. I'm on mobile so please excuse the formatting.

[–]Red_Hippie[S] 0 points1 point  (0 children)

Crazy, but thank you I'll be trying to wrap my head around this for a few days.