4 Replies - 1687 Views - Last Post: 14 October 2008 - 01:02 AM Rate Topic: -----

#1 tgrsnpr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 08

Need help stopping a loop

Posted 12 October 2008 - 08:10 PM

Need help stopping my loop. I thought the if(turn.equalsIgnoreCase("no")) would stop it but it didn't. I had to press ctrl + c to stop it.

  public static void main(String[] args) { 

	String str, turn = "yes";

	//what it does
		System.out.println("This program will average a series of numbers.");

	Scanner keybd = new Scanner(System.in);

	while (turn.equalsIgnoreCase("yes"))
	//user's integers
	System.out.print("Enter an integer (or enter to stop): ");
	String number = keybd.nextLine();
	
	if(turn.equalsIgnoreCase("no"))
	System.out.println();



Is This A Good Question/Topic? 0
  • +

Replies To: Need help stopping a loop

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Need help stopping a loop

Posted 12 October 2008 - 08:13 PM

View Posttgrsnpr, on 12 Oct, 2008 - 08:10 PM, said:

Need help stopping my loop. I thought the if(turn.equalsIgnoreCase("no")) would stop it but it didn't. I had to press ctrl + c to stop it.

  public static void main(String[] args) { 

	String str, turn = "yes";

	//what it does
		System.out.println("This program will average a series of numbers.");

	Scanner keybd = new Scanner(System.in);

	while (turn.equalsIgnoreCase("yes"))
	//user's integers
	System.out.print("Enter an integer (or enter to stop): ");
	String number = keybd.nextLine();
	
	if(turn.equalsIgnoreCase("no"))
	System.out.println();



You need to put braces { } around whatever you want the loop to do. Right now, it's only executing the statement after it an infinite number of times.

I believe you should change it to this.

public static void main(String[] args) { 

	String str, turn = "yes";

	//what it does
		System.out.println("This program will average a series of numbers.");

	Scanner keybd = new Scanner(System.in);

	while (turn.equalsIgnoreCase("yes"))
	{
            //user's integers
	    System.out.print("Enter an integer (or enter to stop): ");
	    String number = keybd.nextLine();
	}
	
        if(turn.equalsIgnoreCase("no"))
	System.out.println();
}


This post has been edited by Locke37: 12 October 2008 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 tgrsnpr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 08

Re: Need help stopping a loop

Posted 12 October 2008 - 11:18 PM

It stops but now it just keeps letting me enter numbers but when I clicked enter it still says enter an integer (or nothing to stop): . How do I get it to stop when someone clicks enter. I thought system.out.println() would work.
	while (turn.equalsIgnoreCase("yes"))
	{
	System.out.print("Enter an integer (or nothing to stop): ");
	String number = keybd.nextLine();
	}
	if(turn.equalsIgnoreCase("no"))
	System.out.println();


Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Need help stopping a loop

Posted 13 October 2008 - 02:06 AM

Welll you can never break out of the the while loop because you enter it when user inputs "yes" however inside it you never actually ask the user if they would like to continue :/

 while (turn.equalsIgnoreCase("yes"))
    {
    System.out.print("Enter an integer (or nothing to stop): ");
    String number = keybd.nextLine();
    }
    if(turn.equalsIgnoreCase("no"))
    System.out.println();



should have something like
 while (turn.equalsIgnoreCase("yes"))
    {
    System.out.print("Enter an integer (or nothing to stop): ");
    String number = keybd.nextLine();
    //check if user wishes to continue
    System.out.println("\nDo you wish to continue (yes / no ): ");
    turn = keybd.next();

    }
    if(turn.equalsIgnoreCase("no"))
    System.out.println();



That way if the user enters anything other than the string "yes" the while loop will break and it will stop asking for integer input

:)

Edit ::
Just something i noticed btw with your main code posted in the first post why are you taking numbers as entered by the user and storing them in String number ? When using JOptionPane you need to do this and then parse them to an Integer using Integer.parseInt(someStringRep);

So just a suggestion for you is to change your input to the use of Integers as you actually suggest in the user prompt... when you use "Enter an Integer (or nothing stop) : "

 public static void main(String[] args) {

    String str, turn = "yes"; 
    int userEnteredInteger = 0;

    //what it does
    System.out.println("This program will average a series of numbers.");

    Scanner keybd = new Scanner(System.in);

    while (turn.equalsIgnoreCase("yes"))
    {
      System.out.print("Enter an integer : ");
      // String number = keybd.nextLine(); // Instead of using this we will use the below
      userEnteredInteger = keybd.nextInt();
     
      // check if user wishes to continue
      System.out.println("\nDo you wish to continue (yes / no ): ");
      turn = keybd.next();
    }
    
    if(turn.equalsIgnoreCase("no"))
    System.out.println();



Now if you are planning on actually adding each integer the user enters and adding them up then u need to add them as each is entered and keep count of how many are entered... Hence a variable int counter = 0; would be a good idea and then after each loop of the while loop increment counter++; (counter = counter + 1)... Also you will need to do something a little different with your variables... you will need to add each integer the user inputs to the total of the previous ones... for example
//in the while loop we have
System.out.println("Enter an Integer : ");
userEnteredInteger = keybd.nextInt()

//that would need modification so that each time the user enters something it is added to
//the previous total of the integers...
//so something like

userEnteredInteger += keybd.nextInt(); 
//this will take the user input and add it to the previous input's.

//after each input increment your counter also
counter++;



So say the user inputs
1 followed by 2, followed by 3
it would operate like this

userEnteredInteger = 0; // 0
userEnteredInteger = userEnteredInteger + 1; // 1
userEnteredInteger = userEnteredInteger + 2; // 3
userEnteredInteger = userEnteredInteger + 3 // 6

Now using the variable counter you can calculate the average by dividing userEnteredInteger by the counter...

System.out.println("Average = " + (userEnteredInteger / counter ) );



Goodluck :pirate:

This post has been edited by bbq: 13 October 2008 - 02:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 devonator  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-September 08

Re: Need help stopping a loop

Posted 14 October 2008 - 01:02 AM

try this instead of using no

	if(turn.equalsIgnoreCase("no"))
	System.out.println();



use an else statement

i got a simple code below from another website its easy to understand, hope it helps you in someway


// This is the Hello program in Java

class Hello {

	public static void main (String args[]) {
	
	  if (args.length > 0) {
		System.out.println("Hello " + args[0]);
	  }
	  else {
		System.out.println("Hello whoever you are.");
	  }
  }

}


This post has been edited by devonator: 14 October 2008 - 01:04 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1