Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java question for beginner

Options
  • 06-12-2012 1:54pm
    #1
    Registered Users Posts: 2,789 ✭✭✭


    hi guys just wondering could someone explain to me what this piece of code is doing i have written an assignment with it in it:

    while(!in.hasNextInt()){
    String i=in.next();
    System.out.println("Value entered was incorrect please enter a number between 1 and 100");
    }
    currentGuess=in.nextInt();


    while(currentGuess<1||currentGuess>100){
    System.out.println("Value entered was incorrect please enter a number between 1 and 100");

    while(!in.hasNextInt()){
    System.out.println("Value entered was incorrect please enter a number between 1 and 100");
    String i=in.next();
    }
    currentGuess=in.nextInt();
    }

    this is all inside another loop i know its looking for validation of type and value of the next input from the user but what happens for the first while loop is it happy that its of right type so it continues cause its all inside a bigger loop then next while statement checks for validation of value,whats my third while loop doing the one repeating the first step,why do i need to repeat it

    sorry about indentation i just copy and pasted
    Tagged:


Comments

  • Registered Users Posts: 42 discodowney


    Uh...if you wrote it surely only you can tell us why you coded it that way?


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Is this the same college assignment we have seen recently in other threads? :rolleyes:


  • Registered Users Posts: 27,121 ✭✭✭✭GreeBo


    Ask the person you copied it off?

    Or, if you are actually interested...tell us what you think its doing on each line and we can help if you are wrong/get stuck.


  • Registered Users Posts: 2,789 ✭✭✭mightyreds


    i dunno what your getting at chrome someone asked to post up college projects people are given cause they wanted to refresh i posted a reply in that nothing wrong with that unless you had a problem , i didn't copy it i was shown this part by the lecturer but couldn't understand it and still cant not that it has anything to do with the question!!

    i know the while loop is looking to see is the next input from the user an integer and if its not i am storing it in a string variable named i which will be thrown away repeat the process till it gets its integer .Then we move on to the second while loop to validate that the input is between 1 and 100 but why do i repeat the first while loop again inside the second while loop bit stuck there.thanks in advance for the help


Advertisement