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

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




Lost in loops

 
Reply to this topicStart new topic

Lost in loops

tgrsnpr
13 Oct, 2008 - 03:17 PM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 11

I really have NO idea how to do looping. I've tried a lot to understand it but I still don't. So far this is wot my code looks like. And it doesn't compile for some reason. And the "" is when I want the loop to end. When someone presses enter the loop ends. But somehow I can't get it to be lik that.

CODE

  public static void main(String[] args) {

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

    Scanner keybd = new Scanner(System.in);

    boolean keepGoing = true;

    while(keepGoing) {
        System.out.print("Enter an integer (or nothing to stop): ");
        int number = keybd.nextInt();
    }
    if(int > "") {
        keepGoing = false;
    }

  }
}

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Lost In Loops
13 Oct, 2008 - 03:26 PM
Post #2

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Your idea is very much correct but the syntax is somewhat off.

A while loop keeps iterating as long as it fulfills the while condition.

CODE

while(keepGoing) {
        System.out.print("Enter an integer (or nothing to stop): ");
        int number = keybd.nextInt();
    }

    if(int > "") {
        keepGoing = false;
    }


When you enter the while-loop, the value of keepgoing = true.
what happens is:

while(keepGoing) {
System.out.print("Enter an integer (or nothing to stop): ");
int number = keybd.nextInt();
}
the program enters the loop and prints the message and reads the input. then it returns to the line
while(keepGoing) {
and evaluates the expression which is true. It will actually never change because if it was to change, it would have to change within the while-loop (which is the four lines above.)

The program never reaches this state. Your idea here was right though
if(int > "") {
keepGoing = false;
}

What you should have done was:

while(int > "") {
System.out.print("Enter an integer (or nothing to stop): ");
int number = keybd.nextInt();
}

(although this is not correct either)

Two problems with the above loop:
* int is a reserved word and can't be used the way you attempted to.
* Even if you had put an integer variable there instead, you couldn't compare it to "".

Usually one assigns a number to mean stop the loop. 0 for example or -1
make the condition compare the variable number to -1 or perhaps:

while(number > 0) {

This post has been edited by Gloin: 13 Oct, 2008 - 03:32 PM
User is offlineProfile CardPM
+Quote Post

tgrsnpr
RE: Lost In Loops
13 Oct, 2008 - 03:33 PM
Post #3

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 11

Would this code be better? I've changed > to .equals
CODE
public static void main(String[] args) {

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

    Scanner keybd = new Scanner(System.in);

    boolean keepGoing = true;

    while(keepGoing) {
        System.out.print("Enter an integer (or nothing to stop): ");
        int number = keybd.nextInt();
    }
    if (keepGoing.equals("")) {
        keepGoing = false;
    }

  }
}

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Lost In Loops
13 Oct, 2008 - 03:37 PM
Post #4

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Unfortunately not.. Try again to read my post. You're mixing up the variabletypes.

Is the requirement that the loop should stop when the user enters nothing?
Because that would force you to read in strings that you convert to doubles. It's a bit more complicated but not alot more writing actually.

Your variable keepGoing is of type boolean and you can't apply the equals-method to a boolean.
In fact you won't even need the keepGoing variable

This post has been edited by Gloin: 13 Oct, 2008 - 03:40 PM
User is offlineProfile CardPM
+Quote Post

tgrsnpr
RE: Lost In Loops
13 Oct, 2008 - 03:58 PM
Post #5

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 11

QUOTE(Gloin @ 13 Oct, 2008 - 04:37 PM) *

Unfortunately not.. Try again to read my post. You're mixing up the variabletypes.

Is the requirement that the loop should stop when the user enters nothing?
Because that would force you to read in strings that you convert to doubles. It's a bit more complicated but not alot more writing actually.

Your variable keepGoing is of type boolean and you can't apply the equals-method to a boolean.
In fact you won't even need the keepGoing variable


Yes, one of the requirements is that the loop should stop when the user hits the enter key. That explains why it gives me this boolean error when I try to compile it. The example the teacher gave us was numbers but I don't see how that helps with this assignment because this one wants it to stop when someone hits the enter key and it gives words and not numbers. Or maybe I'm not looking at it right.
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Lost In Loops
13 Oct, 2008 - 04:18 PM
Post #6

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
You are.. This is how it works..

When you read input from the user, you should read it as words (strings) but you tell the program to interpret the words as numbers. It's called parsing.

CODE

String numStr = "0";
double sum = 0;
int counter = 0;
double number;
while(!numStr.equals("")) {
        number = Double.parseDouble(numStr);
        sum = sum + number
        counter++;
        System.out.print("Enter a number (or nothing to stop): ");
        numStr = keybd.next();
    }
double avg = 0;
if (counter != 1)
  avg = sum / (counter-1);

That's pretty much it.. it's not complete but I hope you can find out the rest yourself.
There's no error-handling in the above code, like if the user enters 'hi!' for example it will crash.

This post has been edited by Gloin: 13 Oct, 2008 - 04:26 PM
User is offlineProfile CardPM
+Quote Post

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

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