12 Replies - 1690 Views - Last Post: 20 April 2015 - 01:20 AM Rate Topic: -----

#1 xander7   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 15-April 15

I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 12:38 PM


public static void getInput(int [] student, int []test1,int []test2,int []test3,
int [] test4, int [] test5)
{ 
    int stuId = 0;
    int t1 = 0;
    int t2 = 0;
    int t3 = 0;      
    int t4 = 0;       
    int t5 = 0; 
    int h = 0;
    char answer = '@';
            
Scanner input = new Scanner(System.in);

        System.out.print(" Will you be inputing records? ");//prompt
       ??? = input.nextint();

        while (??? );

    System.out.print(" insert your student ID ");//prompt
     stuId = input.nextInt();// read student ID

     System.out.print("Insert first test score");//prompt
     t1 = input.nextInt();//read test score
     
     System.out.print("Insert second test score");//prompt
     t2 = input.nextInt();//read test score
     
     System.out.print("Insert third test score");//prompt
     t3 = input.nextInt();//read test score
     
     System.out.print("Insert fourth test score");//prompt
     t4 = input.nextInt();//read test score
     
     System.out.print("Insert fifth test score");//prompt
     t5 = input.nextInt();//read test score
     
     counter for h 
     
     System.out.print(" Will you be inputing more records? ");//prompt
       ??? = input.next();

       student [h]  = stuId;
       test1 [h] = t1;
       test2 [h] = t2;
       test3 [h] = t3;
       test4 [h] = t4;
       test5 [h] = t5;

}

This post has been edited by modi123_1: 15 April 2015 - 12:51 PM
Reason for edit:: please highlight your code, and click the 'code' button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: I need to insert a while-loop to prompt the user for a yes or no

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,461
  • Joined: 12-June 08

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 12:52 PM

Okay - what have you tried, or thought, about for this loop?
Was This Post Helpful? 0
  • +
  • -

#3 DeveloperOfThings   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 89
  • Joined: 12-April 15

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 01:07 PM

Everything inside of a while loop's code body will run and the while loop will keep looping as long as its condition is true.

So think about two things:

1) what to put in the while loop body

2) what condition do you want to use to know if the user wants to keep looping or not
Was This Post Helpful? 0
  • +
  • -

#4 xander7   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 15-April 15

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 01:28 PM

View PostDeveloperOfThings, on 15 April 2015 - 01:07 PM, said:

Everything inside of a while loop's code body will run and the while loop will keep looping as long as its condition is true.

So think about two things:

1) what to put in the while loop body

2) what condition do you want to use to know if the user wants to keep looping or not




I have what i want to be in the body, and i want to ask the user if they have anything else to input. if yes then the program will loop. if no then exit. im just unclear about how to make that into a statement.
Was This Post Helpful? 0
  • +
  • -

#5 DeveloperOfThings   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 89
  • Joined: 12-April 15

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 02:08 PM

Well right now you have while(???);

But a while loop syntax looks like:
while(conditional){
//code that you want to loop goes here
}



So think about this for the condition. You want the program to run if the user types in yes, correct? So the last thing you need to do in your while loop is get input from the user asking if they want to continue. This input is what you want to check in your while loops condition. And you would be checking if the input is "yes". There is a way to check if two strings are equal (not using ==), try to look it up and if you can't then come back here and let me know.

So you need to create a variable (instead of ???) and think about what type it is (for example they will type in a word like yes or no, so is it an int, a double, a String?).

Structure looks like this:
//create variable and get initial input asking if they will be entering records

//while loop starts here and will loop as long as the previous variable mentioned above is equal to "yes"
//while(condition){

//code that you want to loop

//ask if they want to run the program again here, right before ending the while loop, and save input into the same variable mentioned before, the one that you created before while loop
//} end while loop


This post has been edited by DeveloperOfThings: 15 April 2015 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 xander7   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 15-April 15

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 04:05 PM

im stuck. i know the syntax, im trying to make comparisons with things in my book but its only talking about numbers
Was This Post Helpful? 0
  • +
  • -

#7 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 05:45 PM

while (user input is not equal to yes AND user input is not equal to no), {ask for input. }

However, the incomplete code snippets in your code suggest you should be asking for a number, rather than yes or no.

This post has been edited by mike73: 16 April 2015 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 DeveloperOfThings   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 89
  • Joined: 12-April 15

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 15 April 2015 - 06:34 PM

Your input needs to be a String, since you're asking yes/no questions. So ??? needs to be a String variable. And you need to use the appropriate method from your Scanner object to get a String input.

Here is what you can use as a condition for your while loop:

equalsIgnoreCase
Was This Post Helpful? 0
  • +
  • -

#9 kathy025   User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 186
  • Joined: 19-December 14

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 16 April 2015 - 08:52 AM

Note to OP:
|| - means only one condition needs to be true (to enter the loop)
&& - means both conditions must be true (to enter the loop)

View Postmike73, on 16 April 2015 - 08:45 AM, said:

while (user input is not equal to yes OR user input is not equal to no), {ask for input. }

while(answer != 'Y' || answer != 'N') {
	// do something
}


Regarding this, shouldn't it be AND rather than OR?
Using OR will lead to an infinite loop.

Say the correct input is either Y or N.

If the user enters Y, it is NOT N, so it still enters the loop.
If the user enters N, it is NOT Y, so it still enters the loop.

It should be like:
while(answer != 'Y' && answer != 'N') {
	// do something
}


If the user enters Y, only one condition is true, so it will no longer enter the loop.
If the user enters N, only one condition is true, so it will no longer enter the loop.
Was This Post Helpful? 2
  • +
  • -

#10 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 16 April 2015 - 05:28 PM

Yes, you are correct. It's amazing after so many years I still make that stupid mistake :no:
Was This Post Helpful? 0
  • +
  • -

#11 kathy025   User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 186
  • Joined: 19-December 14

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 17 April 2015 - 12:47 AM

Haha, it's okay. It's a common programmer's pitfall. :)
Was This Post Helpful? 0
  • +
  • -

#12 xander7   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 15-April 15

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 19 April 2015 - 07:02 PM

thanks for all the help guys, i went about it a different way. using numbers.


System.out.print(" Will you be inputing records? 0 means no, 1 means yes"
                + " : "
                 );//prompt
       answer = input.nextInt();

       
       
      while(answer == 1)
   
      {
          
        
        System.out.print(" Insert your student ID: ");//prompt
     stuId = input.nextInt();// read student ID

     System.out.print("Insert first test score:");//prompt
     t1 = input.nextInt();//read test score
     
     System.out.print("Insert second test score:");//prompt
     t2 = input.nextInt();//read test score
     
     System.out.print("Insert third test score:");//prompt
     t3 = input.nextInt();//read test score
     
     System.out.print("Insert fourth test score:");//prompt
     t4 = input.nextInt();//read test score
     
     System.out.print("Insert fifth test score:");//prompt
     t5 = input.nextInt();//read test score
     
     
     System.out.print(" Will you be inputing more records? 0 means no, 1 means yes"
            + ": " );//prompt
      answer = input.nextInt();

Was This Post Helpful? 0
  • +
  • -

#13 kathy025   User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 186
  • Joined: 19-December 14

Re: I need to insert a while-loop to prompt the user for a yes or no

Posted 20 April 2015 - 01:20 AM

Here is something I used to do when I was beginning Java:
boolean isLooping = true;
String loopChoice = "";
Scanner scan = new Scanner(System.in);

while(isLooping) { // or do-while
	// do something
	
	
	
	// ask to continue
	while(true) {
		System.out.print("Do you want to try again (Y/N)? ");
		loopChoice = scan.nextLine().trim(); // remove leading and trailing whitespace
		
		if("Y".equalsIgnoreCase(loopChoice)) { // accept either Y or y
			isLooping = true;
			break; // stop inner loop, re-enter outer loop
		
		} else if("N".equalsIgnoreCase(loopChoice)) {
			isLooping = false;
			scan.close(); // close resources
			System.exit(0); // terminate program
		
		} else { // for answers other than Y/y or N/n, re-enter inner loop
			System.out.println("You answer is incorrect.");
		}
	}	
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1