Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
help with the do-wihle loop Posted by paradisojulio on 27 Sept 2010 at 5:24 AM
Hello, I have been having a very hard time with this do while loop...it doesn't seem to work...it says cannot find symbol variable on the last line...can anybody help please?

thank you

System.out.print("Please tell me how many people you are in your group: ");
double numberPeople=input.nextDouble();
System.out.print("Thank you, now tell me the total cost of your meal: ");
double totalCost=input.nextDouble();
System.out.print("Thank you, now please tell me discount you were allotted in % form: ");
double discount=input.nextDouble();


//Compute the given data

do{
double worthDiscount=totalCost*(discount/100);//Calculate the total cost after the discount is given
double amountGst=worthDiscount*GST;//Calculate the amount of Pst (cost after disc.*.05)
double amountQst=(worthDiscount+amountGst)*QST;//Calculate the amount of Qst, so I did (cost after discount+Gst)*.075
double totalPayableAmount=worthDiscount+amountGst+amountQst;
double amountPaidByEach=totalPayableAmount/numberPeople;

//Output the results to console
System.out.println("Your discount is "+discount+"%, which comes up to "+worthDiscount+"$.");
System.out.println("Your subtotal price after discount is "+(totalCost-worthDiscount));
System.out.println("You are "+numberPeople+ " people");
System.out.println("The GST comes to "+amountGst+" $");
System.out.println("The Qst comes to "+amountQst+"$");
System.out.println("Your total price is "+(((totalCost-worthDiscount)*1.05)*1.075)+"$");
System.out.println("The amount paid by each person is: "+(((((totalCost-worthDiscount)*1.05)*1.075))/numberPeople)+"$");

System.out.println("Would you like to repeat the calculation?");
double answer=input.nextDouble();
}
while (answer=1);

}
Report
Re: help with the do-wihle loop Posted by usman riaz on 27 Sept 2010 at 9:11 AM
Use comparison operator '==' instead of assignment operator '=', it will solve your problem.
thanks!
Report
Re: help with the do-wihle loop Posted by usman riaz on 27 Sept 2010 at 9:14 AM
Also declare answer if you haven't!
Report
Re: help with the do-wihle loop Posted by paradisojulio on 27 Sept 2010 at 4:06 PM
Thanks for the help, but when I declare the variable before that, it says that the answer is already defined in main (java.lang.String).

import java.util.Scanner; //Import Scanner

public class Restaurant_Ass1
{

public static void main (String[]args)
{
//Create Scanner
Scanner input=new Scanner(System.in);

//ennumerate my constants
final double GST=0.05;
final double QST=0.075;
double answer;


//Ask for the needed information



System.out.print("Please tell me how many people you are in your group: ");
double numberPeople=input.nextDouble();
System.out.print("Thank you, now tell me the total cost of your meal: ");
double totalCost=input.nextDouble();
System.out.print("Thank you, now please tell me discount you were allotted in % form: ");
double discount=input.nextDouble();


//Compute the given data

do{
double worthDiscount=totalCost*(discount/100);//Calculate the total cost after the discount is given
double amountGst=worthDiscount*GST;//Calculate the amount of Pst (cost after disc.*.05)
double amountQst=(worthDiscount+amountGst)*QST;//Calculate the amount of Qst, so I did (cost after discount+Gst)*.075
double totalPayableAmount=worthDiscount+amountGst+amountQst;
double amountPaidByEach=totalPayableAmount/numberPeople;

//Output the results to console
System.out.println("Your discount is "+discount+"%, which comes up to "+worthDiscount+"$.");
System.out.println("Your subtotal price after discount is "+(totalCost-worthDiscount));
System.out.println("You are "+numberPeople+ " people");
System.out.println("The GST comes to "+amountGst+" $");
System.out.println("The Qst comes to "+amountQst+"$");
System.out.println("Your total price is "+(((totalCost-worthDiscount)*1.05)*1.075)+"$");
System.out.println("The amount paid by each person is: "+(((((totalCost-worthDiscount)*1.05)*1.075))/numberPeople)+"$");

System.out.println("Would you like to repeat the calculation?");
double answer=input.nextDouble();
}
while (answer==1);

}

}
Report
Re: help with the do-wihle loop Posted by usman riaz on 28 Sept 2010 at 12:59 AM
hmmmmm what are the exact outputs can you paste the error msgs?
Report
Re: help with the do-wihle loop Posted by usman riaz on 28 Sept 2010 at 1:05 AM
sorry! i haven't seen that you have already declared the answer before the closing '{' of do while ,just omit the double before the 'answer' variable inside while loop.
Report
Re: help with the do-wihle loop Posted by usman riaz on 28 Sept 2010 at 1:06 AM
i think its ok now take care!
Report
Re: help with the do-wihle loop Posted by paradisojulio on 28 Sept 2010 at 5:19 AM
1000 times, thank you!!
Report
Re: help with the do-wihle loop Posted by paradisojulio on 28 Sept 2010 at 7:37 AM
What you told me works, but now there is another problem...I am trying to make this work... It is a problem where the user must enter an answer in order to restart or finish the program...when I use numbers, it works fine, (1 or 2) but when I use "y" or "n", there are errors that come out:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Restaurant_Ass1.main(Restaurant_Ass1.java:54)

Process completed.

I know this must be a really simple thing, but I have no idea what, I have been trying everything, but it only works with numbers




import java.util.Scanner; //Import Scanner

public class Restaurant_Ass1
{

public static void main (String[]args)
{
//Create Scanner
Scanner input=new Scanner(System.in);

//ennumerate my constants
final double GST=0.05;
final double QST=0.075;
double answer;

//Ask for the needed information


do{ //loop in case answer==y
System.out.print("Please tell me how many people you are in your group: ");
double numberPeople=input.nextDouble();
System.out.print("Thank you, now tell me the total cost of your meal: ");
double totalCost=input.nextDouble();
System.out.print("Thank you, now please tell me the discount you were allotted in % form: ");
double discount=input.nextDouble();


//Compute the given dataSystem.out.printf("Total spendings: %5.2f",total);


double worthDiscount=totalCost*(discount/100);//Calculate the total cost after the discount is given
double amountGst=worthDiscount*GST;//Calculate the amount of Pst (cost after disc.*.05)
double amountQst=(worthDiscount+amountGst)*QST;//Calculate the amount of Qst, so I did (cost after discount+Gst)*.075
double totalPayableAmount=worthDiscount+amountGst+amountQst;// Calculate amount payable in total
double amountPaidByEach=totalPayableAmount/numberPeople;// Calculate amount paid by each member

//Output the results to console
System.out.printf("Your discount is $ %5.2f\n",worthDiscount);
System.out.printf("Your subtotal price after discount is $%5.2f\n",(totalCost-worthDiscount));
System.out.printf("The GST comes to $ %5.2f\n",amountGst);
System.out.printf("The Qst comes to $ %5.2f\n",amountQst);
System.out.printf("Your total price is $ %5.2f\n",(((totalCost-worthDiscount)*1.05)*1.075));
System.out.printf("The amount paid by each person is $ %5.2f\n",(((((totalCost-worthDiscount)*1.05)*1.075))/numberPeople));

do{ //loop in case answer!=y||n

System.out.println("Would you like to repeat the calculation?");
answer=input.nextDouble();
if(answer=='n')
{break;
}
else if (answer!='y')
{System.out.printf("I am sorry, but you input"+answer+"\n This is not valid. Please try again.\n");
}




}
while (answer!='y');

}
while (answer=='y');
}
}


Report
Re: help with the do-wihle loop Posted by usman riaz on 28 Sept 2010 at 9:11 AM
use read() on an inputStreamReader opject for reading a single character(byte[]) at a time....!
Report
Re: help with the do-wihle loop Posted by paradisojulio on 29 Sept 2010 at 5:11 AM
ok, but do I have to import anything first?
Report
This post has been deleted. Posted by paradisojulio on 28 Sept 2010 at 5:21 AM
This post has been deleted.
Report
This post has been deleted. Posted by paradisojulio on 28 Sept 2010 at 5:22 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.