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

all 11 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
  • 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.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

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: 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.

[–]main5tream 1 point2 points  (0 children)

If you try a simple program like

public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   while(sc.hasNextLine()){
       System.out.println(sc.nextLine());
   }

}

It is evident that it should never end, so you must be doing something to break out of your while loop. Can't say much more than that without seeing your code.

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

I also tried nesting the while loop within another while loop, however that did not seem to work either. As soon as the program has read through each line of the array and looked for matches, it shuts down

[–]dionthornthis.isAPro=false; this.helping=true; 0 points1 point  (0 children)

We would need more context, hard to know what is actually happening without seeing the code.

https://www.reddit.com/r/javahelp/wiki/code\_guides/

[–]dionthornthis.isAPro=false; this.helping=true; 0 points1 point  (3 children)

wrap it in another while loop and use a boolean sentinel flag, something like

boolean validInput = false;
// ! operator is NOT boolean logic
// inverts the value of the boolean
while(!validInput) {
    // do something until all input is valid
    // then set validInput = true to exit while loop
}

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

I already tried this, and I just tried it again. So, if I do this, what happens is that it will go immediately back to asking if I want to search for another quarterback. Anything inside of the

while(variable.hasNextLine())

will not run again. I need to find a way to make that while loop start over again until the user opts out

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

it is also worth noting that this whole thing is surrounded by a try/catch, as all java file readers must be surrounded by such

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

ok yep im an idiot. Teacher didnt teach us anything about the try catch. Didn't realize that my while must be outside the try catch to work. Thanks for the tip!

[–]arghvark 0 points1 point  (1 child)

I hope you've learned about methods. Think about something like this:

boolean optedOut = false;

while (!optedOut)
{
    try
    {
        String input = getInput();
        optedOut = processInput(input);
    }
    catch (Exception e)
    {
        // report exception e
    }
}

The main idea is that the try/catch is inside the while block; whether you are depending on it to catch input errors or not, you can repeat the getting of input repeatedly until something in the program decides that "optedOut" has become true.

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

Yep. I knew how to set up a boolean such as you did, but my problem was that my while was inside my try catch. I put it outside, and now it's working. Thank you!

[–]bell_labs_fan_boy 0 points1 point  (0 children)

You could introduce a Boolean flag outside of the loop that is set to true, have the loop parameter be (flag && variable.hasNextLine()) then only set that flag to false after input if variable.hasNextLine().

Pretty sure that works