-
java problem
I am programming in java and i just started out in college.
For my class i have to write a program with the if/else statements
i have this so far and im getting the following errors
System.out.print("Enter the length of the desk : ");
int length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
int width=MyInput.readInt();
int area = (length * width)
System.out.println("Enter 1 for pine wood");
System.out.println("Enter 2 for oak wood");
System.out.println("Enter 3 for mahogany wood");
System.out.print("Enter the type of wood: ");
int wood = MyInput.readInt();
if (wood == 2)
(desk+100);
else
(desk+150);
if (wood == 1)
desk = 200);
System.out.print("Enter the number of drawers: ");
int wood= MyInput.readInt();
lab3_141.java:20: ';' expected
System.out.print("Enter the number of drawers: ");
^
lab3_141.java:23: not a statement
desk+75;
^
lab3_141.java:25: not a statement
desk+150; }
^
3 errors
any suggestions?
-
Redirect your java-related questions to http://forum.java.sun.org
While you're asking them here, try using this forums code-tags to format your code, it'll help others to read your code clearly.
Your first problem seems to be this: there's no ; after this line: int area = (length * width) Furthermore, you didn't declare a variable with the name 'desk'. At last, lines like these have no meaning:
Did you mean desk=150; ?
-
desk+100; isn't a proper Java statement. desk = desk + 100; will do what you want. That takes care of two errors.
lab3_141.java:20: ';' expected
System.out.print("Enter the number of drawers: ");
That's because of this statement:
desk = 200);
The ; should be after the 200.
-
even when i declare desk to be 200 up top, it still has the same errors
-
Paste your entire code here. It'll be easier to understand what to fix.
-
ytyes
AH TT your a savior, thats what it was, thank you much
^
-
/me bows.
Think I need to edit this, your code is horriblely written. Post it here and we'll show you how to keep it nice and clean.
-
int drawer= MyInput.readInt();
int drawers = drawer * 30;
if (area > 1000)
desk = desk+75;
if (area > 2000)
desk = desk+125;
else
desk=desk;
if (wood == 2)
desk=desk+100;
if (wood == 3)
desk=desk+150;
else
desk=desk;
System.out.println("The cost of your desk is: " + (desk+drawers));
Are my If/elses ok, b/c im not gettin the correct calculation
-
here this is my whole code
import java.io.* ;
public class lab3_141
{
public static void main(String[] args)
{
int desk=200;
System.out.print("Enter the length of the desk : ");
int length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
int width=MyInput.readInt();
int area = (length * width);
System.out.println("Enter 1 for pine wood");
System.out.println("Enter 2 for oak wood");
System.out.println("Enter 3 for mahogany wood");
System.out.print("Enter the type of wood: ");
int wood = MyInput.readInt();
System.out.print("Enter the number of drawers: ");
int drawer= MyInput.readInt();
int drawers = drawer * 30;
if (area > 1000)
desk = desk+75;
if (area > 2000)
desk = desk+125;
else
desk=desk;
if (wood == 2)
desk=desk+100;
if (wood == 3)
desk=desk+150;
else
desk=desk;
System.out.println("The cost of your desk is: " + (desk+drawers));
}
}
-
This is what it is supposed to do
Purpose : To write a simple program to perform input, output and use control structures in Java.
Description : Write a program to that calculates and displays prices for custom made desks.The base price for a desk is $200.00. Our desks are built using three types of wood, pine, oak, and mahogany. Add $100.00 to the cost for oak and $150.00 for mahogany. No extra charge for pine. If the surface area of the desk is over 1000 square inches, add $75.00. If the surface area of the desk is over 2000 square inches add $125.00. There is a $30 charge for each drawer in the desk. Compute the price display the values. Use a switch statement to determine cost for wood type.
-
Hm, could you explain what the program is trying to do and post the ENTIRE code, including the public class statements and import statements. You seem to be using cs1 for input, am I correct?
-
i believe so, i just started this class in winter quater. i posted the code and what it is supposed to do on page one, help me clean that sucker up plz
-
Now I didn't bother checking if your code was correct (i.e. if it solved the problem) because I think that's something you should learn to do yourself. But I wanted to let you see how I would have laid it out. Basically, I normally divide my code into logical sections (i.e. a print asking for input and then an input statement, then a new line, etc.) I also keep if-then-else statements together, so applying that to your code I get:
Code:
import java.io.* ;
public class lab3_141
{
public static void main(String[] args)
{
int desk=200;
System.out.print("Enter the length of the desk : ");
int length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
int width=MyInput.readInt();
int area = (length * width);
System.out.println("Enter 1 for pine wood");
System.out.println("Enter 2 for oak wood");
System.out.println("Enter 3 for mahogany wood");
System.out.print("Enter the type of wood: ");
int wood = MyInput.readInt();
System.out.print("Enter the number of drawers: ");
int drawer= MyInput.readInt();
int drawers = drawer * 30;
if (area > 1000)
desk = desk+75;
if (area > 2000)
desk = desk+125;
else
desk=desk;
if (wood == 2)
desk=desk+100;
if (wood == 3)
desk=desk+150;
else
desk=desk;
System.out.println("The cost of your desk is: " + (desk+drawers));
}
}
Now when you're posting code to this forum, what you should do is arrange it with tabs and newlines, etc in a text editor, then paste it into the reply box and put "["code"]" before it and "["/code"]" after it (without the quotes. I put them in so that the tags would show up.
And remember, you don't need to copy my layout exactly, just arrange your code in sections so that it is easier to read and stick to however you arrange it with all your programs.
ac
-
Code:
import cs1.Keyboard;
public class ifStatementPractice {
public static void main(String args[]) {
int price = 200;
int width;
int length;
int surface;
int wood;
int drawers;
System.out.print("Enter table surface's width (inches): ");
width = Keyboard.readInt();
System.out.print("Enter table surface's length (inches): ");
length = Keyboard.readInt();
surface = width * length;
if((surface > 1000) && (surface < 2000)) {
price += 75;
}
if(surface > 2000) {
price += 125;
}
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Which type of wood: ");
wood = Keyboard.readInt();
switch(wood) {
case 1:
price += 100;
case 2:
price += 150;
case 3:
price += 0;
}
System.out.print("How many drawers would you like - $30/drawer: ");
drawers = Keyboard.readInt();
price += (drawers * 30);
System.out.println("Your new desk is going to cost: $" + price);
}
}
I haven't tested the code, but if you're gonna use it, you're gonna have to switch up the input statements since I use a third party package. If anyone notices anything wrong, drop me a line.
-
Wow thanks guys, that looks much better than mine and it helps to see whats going
Thanks for the input
-
No problem, if you have any questions, please do post, we'll be glad to help.
-
I put in my information needed and took out the switch ( so i understand if/else) and it now looks like this
Code:
import java.io.* ;
public class try
{
public static void main(String args[])
{
int price = 200;
int width;
int length;
int surface;
int wood;
int drawers;
System.out.print("Enter table surface's width (inches): ");
width = MyInput.readInt();
System.out.print("Enter table surface's length (inches): ");
length = MyInput.readInt();
surface = width * length;
if((surface > 1000) && (surface < 2000)) {
price += 75;
}
if(surface > 2000) {
price += 125;
}
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Which type of wood: ");
wood = MyInput.readInt();
if (wood == 1) {
price += 100;
}if (wood == 2) {
price += 150;
}if (wood == 3) {
price += 0;
}
System.out.print("How many drawers would you like - $30/drawer:$
drawers = MyInput.readInt();
price += (drawers * 30);
System.out.println("Your new desk is going to cost: $" + price);
}
}
I am gettin this error with it and im gettin a bit flustered.
Code:
try.java:3: <identifier> expected
public class try
^
try.java:52: '{' expected
^
2 errors
The way i read it is that I am missing a { or } but i counted and i have 7 { and 7 } lol so i dont know where to look next.
i dont understand about the switch just yet, (forgot to mention that we were supposed to only use if/else statements) but i re structured my program and it looks a bit nicer,
P.S. im not using TT's program for class, i already have one,and my profesor would know i didnt do it lol, im just tryin to understand some of the errors.
-
try is a Java keyword, you need a new name for your program.
System.out.print("How many drawers would you like - $30/drawer:$
That line appears to be slaughtered. No clue what you were trying to do. Look into it. =)
-
ah, that was it. and i fixed the other problem. Ok this is the final one as far as i know it compiles and runs
Code:
import java.io.* ;
public class lab3_141
{
public static void main(String[] args)
{
double area;
double wood;
double width;
double length;
double drawer;
double price = 200;
System.out.print("Enter the length of the desk : ");
length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
width=MyInput.readInt();
area = (length * width);
if (area > 1000)
price += 75;
if (area > 2000)
price += 125;
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Enter the type of wood: ");
wood = MyInput.readInt();
System.out.print("Enter the number of drawers: ");
drawer= MyInput.readInt();
if (wood == 2)
price += 100;
if (wood == 3)
price += 150;
else
price += 0;
price += (drawer * 30);
System.out.println("The cost of your desk is: $" + price );
}
}
Thank you for everythin, i actually learned alot expecially about cleaning up the program. I hope you take one final look at it and let me know if i can do anything else to better my technique.
-
Code:
if (area > 1000)
price += 75;
if (area > 2000)
price += 125;
Well I see that as a potential interpreting issue. When I originally coded the program I was under the assumption that if the surface area is over 1000 sq. inches that a fee of $75 would be added and if the surface area is over 2000 sq inches, that a $125 would be added and only a $125, not the $75 as well. With your current code, you will be charging the customer $200 extra. But I guess that is an interpretation issue, not coding. Otherwise it seems fine. Glad you learned something.
-
well turns out there is problems
this is mine
Code:
// File: lab3_141.java
// Jeremy Otis
// 08
import java.io.* ;
public class lab3_141
{
public static void main(String[] args)
{
double area;
double wood;
double width;
double length;
double drawer;
double price = 200;
System.out.print("Enter the length of the desk : ");
length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
width=MyInput.readInt();
area = (length * width);
if (area > 1000)
price += 75;
if (area > 2000)
price += 125;
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Enter the type of wood: ");
wood = MyInput.readInt();
if (wood == 1)
price += 100;
if (wood == 2)
price += 150;
if (wood == 3)
price += 0;
System.out.print("Enter the number of drawers: ");
drawer= MyInput.readInt();
price += (drawer * 30);
System.out.println("The cost of your desk is: $" + price );
}
}
when i enter the values 60=leng 48=wid 1=oak 4=drawers
i get the answer 620
However on your program (with slight modifications)
Code:
import java.io.* ;
public class labredo
{
public static void main(String[] args)
{
int price = 200;
int width;
int length;
int surface;
int wood;
int drawers;
System.out.print("Enter table surface's width (inches): ");
width = MyInput.readInt();
System.out.print("Enter table surface's length (inches): ");
length = MyInput.readInt();
surface = width * length;
if((surface > 1000) && (surface < 2000)) {
price += 75;
}
if(surface > 2000) {
price += 125;
}
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Which type of wood: ");
wood = MyInput.readInt();
if (wood == 1) {
price += 100;
}if (wood == 2) {
price += 150;
}if (wood == 3) {
price += 0; }
System.out.print("How many drawers would you like - $30/drawer:$
drawers = MyInput.readInt();
price += (drawers * 30);
System.out.println("Your new desk is going to cost: $" + price);
}
}
Using the same values i get 545 which is correct.
What math am i doing wrong?
-
it was the
if ((area > 1000) && (area <2000) statement
however i dont really understand what that did
sorry if im kind of scatter-brained, i keep fixing and finding new problems. This time im done but i dont understand what the difference was
-
You're charging them $75 too much like I said in my previous post. You're charging them a $200 fee rather than just a $125. Look at my original and the double condition if statement I have, you should do something similar to it.
That compund if statement is basically an if with two conditions. Translated into words it means: if the area is greater than 1000 AND ( && ) the area is less than 2000, then perform what's under this if statement.
-
oh, tha makes sense then. I changed mine to something similar. Now i might have a bit of an edge in that class. I appreciate everything