9 Replies - 3207 Views - Last Post: 21 April 2009 - 03:38 PM Rate Topic: -----

#1 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

User defined exit from loop

Posted 20 April 2009 - 09:54 AM

Here is the code that I have written. I am having trouble with requesting the user to continue the loop or exit. Any help would be greatly appreciated. I have narrowed the problem down to the following bit of code with in the whole project.

Here is the problem child. Any ideas?

	   System.out.println("Do you Have more Shopping? Y to continue any other key to exit: ");
	   prod = (char)input.nextInt();



Here is the whole code

import java.util.Scanner;
/**
 *
 * @author macowner
 */
public class prodsales {


	public static void main(String[] args)
	{

	   // Variable Decalrations
	   int prodNum = 0;
	   int quanSold = 0;
	   double totalSale = 0;
	   double sale = 0;
	   double cost1 = 2.98;
	   double cost2 = 4.50;
	   double cost3 = 9.98;
	   double cost4 = 4.49;
	   double cost5 = 6.87;
	   char prod = 0;

	   Scanner input = new Scanner (System.in);
	   
	   
	   while (prod != 'Y')
	   {
			/*
			 * retrieves input of Product number from usr while
			 * error trapping any product that is not 1 - 5
			 */
			System.out.print("Enter Product Number 1-5: ");
			prodNum = input.nextInt();
			while (prodNum <1 || prodNum > 5)
			{
				System.out.println("Invalid Product: Enter Product Number 1-5: ");
				prodNum = input.nextInt();
			}

			/* retrieves input of 'Quantity Sold' form the user while
			*  error trapping any quantity sold that is less than 1
			*/
			System.out.print("Enter Quantity Sold: ");
			quanSold = input.nextInt();
			while (quanSold <1)
			{
				System.out.println("Invalid Quantity: Enter Quantity Sold: ");
				quanSold = input.nextInt();
			}

			switch (prodNum)
			{
				case 1:										 //total cost for Product 1
				sale = (double)quanSold * cost1;
				break;

				case 2:
				sale = (double)quanSold * cost2;				//total cost for Product 2
				break;

				case 3:
				sale = (double)quanSold * cost3;				//total cost for Product 3
				break;

				case 4:
				sale = (double)quanSold * cost4;				//total cost for Product 4
				break;

				case 5:
				sale = (double)quanSold * cost5;				//total cost for Product 5
				break;
		   }
			  
	   totalSale =+ sale;//Overall total of sales
	   System.out.printf("Total Sales for this Product: $%.2f\n", sale);
	   System.out.println("Do you Have more Shopping? Y to continue any other key to exit: ");
	   prod = (char)input.nextInt();
	   }
	   System.out.print("\nTotal Cost: " + totalSale);		  //Prints the Total Sales
	   System.out.print("\n\n");



Is This A Good Question/Topic? 0
  • +

Replies To: User defined exit from loop

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2773
  • View blog
  • Posts: 15,869
  • Joined: 24-September 08

Re: User defined exit from loop

Posted 20 April 2009 - 10:02 AM

Do a nextLine and have a while loop. while(prod == y) with prod being a String.
Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: User defined exit from loop

Posted 20 April 2009 - 11:22 AM

I suggest using a do...while loop in your case, e.g.
do{
//your code, including input wether to continue or stop, i.e. get prod
}while(prod == 'y');


Hope this helps, good luck ;)

This post has been edited by ayman_mastermind: 20 April 2009 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: User defined exit from loop

Posted 20 April 2009 - 05:42 PM

Thanks, I didn't see that problem. However, I am still having problem with this line of code.

prod = (char)input.nextInt();



I am pretty sure that this is causing my error but am uncertain as to what is wrong or how to fix it. :crazy:

here is the error that I recieve when I debug it in NetBeans

debug:
Enter Product Number 1-5: 1
Enter Quantity Sold: 2
Total Sales for this Product: $5.96
Do you Have more Shopping? Y to continue any other key to exit: 
y
Exception in thread "main" java.util.InputMismatchException
		at java.util.Scanner.throwFor(Scanner.java:819)
		at java.util.Scanner.next(Scanner.java:1431)
		at java.util.Scanner.nextInt(Scanner.java:2040)
		at java.util.Scanner.nextInt(Scanner.java:2000)
		at prodsales.main(prodsales.java:82)
Java Result: 1




Am I even writing this code correctly? I have a hard time with the char command. Does anyone see where I am going wrong?

Thanks for all the help so far!

~Bruce
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2773
  • View blog
  • Posts: 15,869
  • Joined: 24-September 08

Re: User defined exit from loop

Posted 20 April 2009 - 07:25 PM

Did you not even read my post?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: User defined exit from loop

Posted 20 April 2009 - 08:37 PM

PBL's rule #1 when using a Scanner to read from System.in

If you are NOT expecting to read multiple int/double/String/float/boolean from a single line
ALWAYS do String line = scanner.nextLine();
and then convert from String to int/float/double/....

nextDouble() or nextInt() do not read the endOfLine at the end of the line so all the hasNextXXXX and nextXXXX might produce unexpected results

If you are in a scenario where you read only ONE value from the console
- read the whole line with nextLine()
- translate using a try/catcg(NumberFormatException e) block
Was This Post Helpful? 0
  • +
  • -

#7 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: User defined exit from loop

Posted 21 April 2009 - 09:52 AM

View PostNeoTifa, on 20 Apr, 2009 - 06:25 PM, said:

Did you not even read my post?

Sorry NeoTifa,

I did not see your post. :D However, when I tried to a next line with the prod being a string I got an error of incompatible types (String and char). This is what I have been fighting with the whole time.

~Bruce
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2773
  • View blog
  • Posts: 15,869
  • Joined: 24-September 08

Re: User defined exit from loop

Posted 21 April 2009 - 11:47 AM

Don't parse it! Like I said, make prod a string and have a while loop saying while(prod != "y" || prod != "Y") {}
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: User defined exit from loop

Posted 21 April 2009 - 02:10 PM

View PostNeoTifa, on 21 Apr, 2009 - 10:47 AM, said:

Don't parse it! Like I said, make prod a string and have a while loop saying while(prod != "y" || prod != "Y") {}

You don't compare strings like that anyways, you use equals()

@mobius
Why not just make the char at?
do{
//all your code
System.out.print("Would ou like to keep shopping? Y or N");
String answer = in.nextLine();
}while(answer.charAt(0) == ('Y' || 'y'));


Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#10 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: User defined exit from loop

Posted 21 April 2009 - 03:38 PM

Hey Fuzzyness and NeoTifa,

Thanks for all the help. Between both of your posts I was able to piece it together.

~Bruce
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1