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

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




Looping a phrase

 
Reply to this topicStart new topic

Looping a phrase

tgrsnpr
post 10 Oct, 2008 - 01:36 PM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 11

I need help with looping a phrase. And when the user doesn't enter anything then the loop ends. I know that I have to use a while {} but other than that I have no idea how to start this and end the loop, so could anyone please help me.
User is offlineProfile CardPM

Go to the top of the page

markhazlett9
post 10 Oct, 2008 - 01:39 PM
Post #2


D.I.C Head

**
Joined: 12 Jul, 2008
Posts: 132



Thanked 8 times
My Contributions


QUOTE(tgrsnpr @ 10 Oct, 2008 - 02:36 PM) *

I need help with looping a phrase. And when the user doesn't enter anything then the loop ends. I know that I have to use a while {} but other than that I have no idea how to start this and end the loop, so could anyone please help me.


Do you have any code... It's dreamincode's policy to not assist users before a valid effort is put into the code. Please post your code like this...

CODE
Code here
User is offlineProfile CardPM

Go to the top of the page

nick2price
post 10 Oct, 2008 - 01:43 PM
Post #3


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 231



Thanked 6 times
My Contributions


put all your code in a do block and then have an arguement in your whiles parameter that will keep it going. If this arguement is broken, the loop will end.
User is offlineProfile CardPM

Go to the top of the page

tgrsnpr
post 10 Oct, 2008 - 01:49 PM
Post #4


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 11

QUOTE(markhazlett9 @ 10 Oct, 2008 - 02:39 PM) *

QUOTE(tgrsnpr @ 10 Oct, 2008 - 02:36 PM) *

I need help with looping a phrase. And when the user doesn't enter anything then the loop ends. I know that I have to use a while {} but other than that I have no idea how to start this and end the loop, so could anyone please help me.


Do you have any code... It's dreamincode's policy to not assist users before a valid effort is put into the code. Please post your code like this...

CODE
Code here



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

    //user's integers(loop)
    System.out.print("Enter an integer (or nothing to stop): ");
    String number = keybd.nextLine();


this is all that i got
User is offlineProfile CardPM

Go to the top of the page

markhazlett9
post 10 Oct, 2008 - 01:50 PM
Post #5


D.I.C Head

**
Joined: 12 Jul, 2008
Posts: 132



Thanked 8 times
My Contributions


QUOTE(tgrsnpr @ 10 Oct, 2008 - 02:49 PM) *

QUOTE(markhazlett9 @ 10 Oct, 2008 - 02:39 PM) *

QUOTE(tgrsnpr @ 10 Oct, 2008 - 02:36 PM) *

I need help with looping a phrase. And when the user doesn't enter anything then the loop ends. I know that I have to use a while {} but other than that I have no idea how to start this and end the loop, so could anyone please help me.


Do you have any code... It's dreamincode's policy to not assist users before a valid effort is put into the code. Please post your code like this...

CODE
Code here



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

    //user's integers(loop)
    System.out.print("Enter an integer (or nothing to stop): ");
    String number = keybd.nextLine();


this is all that i got


are you receiving any errors? What is the issue?
User is offlineProfile CardPM

Go to the top of the page

TriggaMike
post 10 Oct, 2008 - 02:08 PM
Post #6


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 40



Thanked 3 times
My Contributions


Structure it like this
CODE
do
{
code and stuff here
}while(yourVariable.length() > 0);


The only thing with this is yourVariable needs to be a string, and you will have to parse it and add it to your running total. I don't know if there is a way to do a comparison for type int similar to this.

This post has been edited by TriggaMike: 10 Oct, 2008 - 02:09 PM
User is offlineProfile CardPM

Go to the top of the page

nick2price
post 10 Oct, 2008 - 02:18 PM
Post #7


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 231



Thanked 6 times
My Contributions


Shouldnt really do it like that because he should be dealing with ints and ints cannot be dereferenced with the .length method. I have changed your program a little bit to get you started. Instead of exiting on null input, i have used 0 to exit. You should be able to see why it is best to use ints.
CODE
import java.util.Scanner;


public class Average{
    
  public static void main(String[] args) {
  
  int number = 0;
  int sum = 0;
  int count = 0;

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

    Scanner keybd = new Scanner(System.in);

    
    do
    {
    System.out.println("Enter an integer (or 0 to stop): ");
    number = keybd.nextInt();
    sum=sum+number;
    count++;
    }
    
    while(number>0);

    System.exit(0);

    

}
}


Now after the while, you still have to work out the average. The int sum now holds the total of all inputs. Count now holds the number of inputs. average = total/number. You should be able to figure it out.
User is offlineProfile CardPM

Go to the top of the page

TriggaMike
post 10 Oct, 2008 - 02:32 PM
Post #8


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 40



Thanked 3 times
My Contributions


I said it had to be of type String, and the person requesting specifically said they wanted exit on user entering nothing. I also tried to stick to forum rules and gave the user a concept not a working program. I knew about ways to use sentinel values, I was just saying that was the only way I knew for entering nothing to exit a loop.

This post has been edited by TriggaMike: 10 Oct, 2008 - 02:33 PM
User is offlineProfile CardPM

Go to the top of the page

nick2price
post 10 Oct, 2008 - 02:51 PM
Post #9


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 231



Thanked 6 times
My Contributions


You must of edited it after i started a reply. The code i have provided is different to what the user wanted. As you say, they want it to exit on no input, this can be done with int, but the way i know is quite difficult as you have to use the primative data type. What i am trying to do is show the concept of how a while loop worls and how you would store the different values. He will have to work out how to get the average for these.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 11:18AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month