Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,226 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,340 people online right now. Registration is fast and FREE... Join Now!




Need help stopping a loop

 
Reply to this topicStart new topic

Need help stopping a loop

tgrsnpr
12 Oct, 2008 - 07:10 PM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 11

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.

CODE

  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();



User is offlineProfile CardPM
+Quote Post

Locke37
RE: Need Help Stopping A Loop
12 Oct, 2008 - 07:13 PM
Post #2

I'm not a thief...I prefer the term TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 1,002



Thanked: 39 times
Dream Kudos: 325
My Contributions
QUOTE(tgrsnpr @ 12 Oct, 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.

CODE

  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.

java
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 Oct, 2008 - 07:14 PM
User is offlineProfile CardPM
+Quote Post

tgrsnpr
RE: Need Help Stopping A Loop
12 Oct, 2008 - 10:18 PM
Post #3

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 11

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

    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();


User is offlineProfile CardPM
+Quote Post

bbq
RE: Need Help Stopping A Loop
13 Oct, 2008 - 01:06 AM
Post #4

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
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 confused.gif

java

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
java

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

smile.gif

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) : "

java

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
java

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

java

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


Goodluck pirate.gif




This post has been edited by bbq: 13 Oct, 2008 - 01:22 AM
User is offlineProfile CardPM
+Quote Post

devonator
RE: Need Help Stopping A Loop
14 Oct, 2008 - 12:02 AM
Post #5

New D.I.C Head
*

Joined: 6 Sep, 2008
Posts: 43


My Contributions
try this instead of using no
CODE


    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

CODE


// 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 Oct, 2008 - 12:04 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:05AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month