# Scanner and looping question

Page 1 of 1

## 7 Replies - 1358 Views - Last Post: 13 March 2007 - 12:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=24855&amp;s=579e7a3de14d637f75bfc60ee1f8d8d5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sandy2002w

Reputation: 0
• Posts: 4
• Joined: 08-March 07

# Scanner and looping question

Posted 08 March 2007 - 12:19 PM

I'm very new to programing anid I'm trying to figure out what is the best looping method to use for a scanner.

When you enter a Value for the scanner it will check to see if the Price is between two values. If it good it does the calculations, if the Price is to high it will gave a flag tell you you need to re enter a new value. Question is what looping method do I use. Do while, if, else...
Here is what I have.
```//Make a scanner for price
//make a Scanner object for Price
Scanner kbd2 = new Scanner(System.in);

// prompt the user for data and start adding it to the object
//add loop info here if wrong price
System.out.print("\nEnter the Price: ");
_flag = kbd2.nextDouble();

//compare Price with scanner

if
(price_flag > Price_set1 || price_flag < Price_set2); // see if price is less than 10000

if (price_flag == Price_set2 || price_flag > Price_set2) // cal if Price 10000 is greater than amount enter
System.out.print("Incorrect Price - try again");

//tax on product create a new variables to hold the data
Tax = price_flag;
Tax = ((int)Tax * 0.08); // taxes on product

```

Thanks
Sandy

This post has been edited by sandy2002w: 08 March 2007 - 12:24 PM

Is This A Good Question/Topic? 0

## Replies To: Scanner and looping question

### #2 tikiman1023

Reputation: 0
• Posts: 5
• Joined: 08-March 07

## Re: Scanner and looping question

Posted 08 March 2007 - 12:32 PM

Probably your best bet would to be to do something like this.

Create the Scanner object
Read in first price (So that you have something for the initial check of the while loop)
Make a while loop that checks if price is between the specified values and keeps looping until the price is
Inside the while loop make an if statement that prints to the console the price is invalid if it is outside the range and then asks for a new value.

Didn't give ya any code because I found it was much better when doing simple beginning programs like this that you do all the code on your own and just use conceptual ideas to help you along the way. At least it helped me a lot

### #3 sandy2002w

Reputation: 0
• Posts: 4
• Joined: 08-March 07

## Re: Scanner and looping question

Posted 13 March 2007 - 10:49 AM

tikiman1023:
Thanks for the tip, but I'm still lost...

Would like to know after the while statement do you place

while (true)
or
while (price_flag != 100000) // I did this one and I got a endless loop

thanks
damon
PS after the Price with scanner I have another scanner that needs to find the value.

This post has been edited by sandy2002w: 13 March 2007 - 10:50 AM

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Scanner and looping question

Posted 13 March 2007 - 10:59 AM

Can you post your updated code?

### #5 sandy2002w

Reputation: 0
• Posts: 4
• Joined: 08-March 07

## Re: Scanner and looping question

Posted 13 March 2007 - 11:03 AM

Here is the revised code;

```//Make a scanner for price
//			 make a Scanner object for Price
Scanner kbd2 = new Scanner(System.in);
{
//	 prompt the user for data and start adding it to the object
//add loop info here if wrong price
System.out.print("\nEnter the Price: ");
price_flag = kbd2.nextDouble();

//	 compare Price with scanner
while (price_flag != 10000.00){
if  (price_flag > Price_set1 || price_flag < Price_set2);
// see if price is equals or is more than 10000
else if  (price_flag == Price_set2 || price_flag > Price_set2) // cal if Price 10000 is greater than amount enter

System.out.print("Incorrect Price - try again");
}
//	tax on product create a new variables to hold the data
Tax = price_flag;
Tax = ((int)Tax * 0.08); // taxes on product

//Setting the values for price
public static final double Price_set1 = 0;
public static final double Price_set2 = 10000.00;
}

```

This post has been edited by sandy2002w: 13 March 2007 - 11:08 AM

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Scanner and looping question

Posted 13 March 2007 - 11:31 AM

Assuming you are trying to run a loop that will keep prompting the user for a price if the initial price is greater than 10000, you can use something like the following:
```price_flag = kbd2.nextDouble();
while(price_flag>=10000)
{
System.Out.Println("Please enter a price below 10000:");
price_flag = kbd2.nextDouble();
}

```

### #7 sandy2002w

Reputation: 0
• Posts: 4
• Joined: 08-March 07

## Re: Scanner and looping question

Posted 13 March 2007 - 11:59 AM

so there is no need to test for this;
if (price_flag > Price_set1 || price_flag < Price_set2);

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Scanner and looping question

Posted 13 March 2007 - 12:05 PM

Depends what your condition is...if you want to re-prompt for input if the price is not between two specific prices, then yes, we will have to amend the condition.