7 Replies - 990 Views - Last Post: 13 March 2007 - 12:05 PM Rate Topic: -----

#1 sandy2002w  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

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

#3 sandy2002w  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Scanner and looping question

Posted 13 March 2007 - 10:59 AM

Can you post your updated code?
Was This Post Helpful? 0
  • +
  • -

#5 sandy2002w  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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();
}


Was This Post Helpful? 0
  • +
  • -

#7 sandy2002w  Icon User is offline

  • New D.I.C Head

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

Re: Scanner and looping question

Posted 13 March 2007 - 11:59 AM

Thanks Amadeus
so there is no need to test for this;
if (price_flag > Price_set1 || price_flag < Price_set2);
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1