8 Replies - 624 Views - Last Post: 10 October 2008 - 03:51 PM Rate Topic: -----

#1 tgrsnpr  Icon User is offline

  • New D.I.C Head

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

Looping a phrase

Post icon  Posted 10 October 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.
Is This A Good Question/Topic? 0
  • +

Replies To: Looping a phrase

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Looping a phrase

Posted 10 October 2008 - 02:39 PM

View Posttgrsnpr, on 10 Oct, 2008 - 02:36 PM, said:

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 here

Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Looping a phrase

Posted 10 October 2008 - 02:43 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 tgrsnpr  Icon User is offline

  • New D.I.C Head

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

Re: Looping a phrase

Posted 10 October 2008 - 02:49 PM

View Postmarkhazlett9, on 10 Oct, 2008 - 02:39 PM, said:

View Posttgrsnpr, on 10 Oct, 2008 - 02:36 PM, said:

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 here


  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
Was This Post Helpful? 0
  • +
  • -

#5 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Looping a phrase

Posted 10 October 2008 - 02:50 PM

View Posttgrsnpr, on 10 Oct, 2008 - 02:49 PM, said:

View Postmarkhazlett9, on 10 Oct, 2008 - 02:39 PM, said:

View Posttgrsnpr, on 10 Oct, 2008 - 02:36 PM, said:

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 here


  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?
Was This Post Helpful? 0
  • +
  • -

#6 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Looping a phrase

Posted 10 October 2008 - 03:08 PM

Structure it like this
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 October 2008 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Looping a phrase

Posted 10 October 2008 - 03:18 PM

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.
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.
Was This Post Helpful? 0
  • +
  • -

#8 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Looping a phrase

Posted 10 October 2008 - 03:32 PM

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 October 2008 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Looping a phrase

Posted 10 October 2008 - 03:51 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1