Results 1 to 20 of 25
- 08-21-2009, 05:35 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
Newbie need help in if ... else stament
when i run the java file..the if...else cant function
when i key in any number...it apear the error message
"Wrong Enter"
i key in 1 oso appear that message:confused:
Java Code:import java.io.*; import java.sql.*; class Test { public static void main(String[] args)throws IOException { String choice=""; String CustName,EmpName; int VenuePrice,MenuPrice,NoSet,Payment,WorkHour; int HourlyRate,Salary,MenuCode,Venue,Quantity; String CustID,CustAddress,CustContact,CustAge,CustOrderNumber,CustGender; String DT,OrderNo,EmpID,EmpIC,EmpAddress,EmpPosition,EmpGender,EmpStatus,EmpHourlyRate; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("======================================================"); System.out.println("| Welcome !!! |"); System.out.println("======================================================"); System.out.println("1.Enter Cusomer Data."); System.out.println("2.Enter Order"); System.out.println("3.Enter Employee Data."); System.out.println("4.Check Menu."); System.out.println("5.Check Venue."); System.out.println("6.Calculate Customer Payment"); System.out.println("7.Calculate Employee Salary"); System.out.println("Enter the No"); choice=br.readLine(); if (choice=="1") { System.out.println("======================================================"); System.out.println("| Employee |"); System.out.println("======================================================"); System.out.println("Enter Customer ID"); CustID=br.readLine(); System.out.println("Enter Customer Name"); CustName=br.readLine(); System.out.println("Enter Customer Address"); CustAddress=br.readLine(); System.out.println("Enter Customer Contact"); CustContact=br.readLine(); System.out.println("Enter Customer Age"); CustAge=br.readLine(); System.out.println("Enter Customer Order Number"); CustOrderNumber=br.readLine(); System.out.println("Enter Customer Gender"); CustGender=br.readLine(); System.out.println("\t\tCustomer Detail\t\t"); System.out.println("\tCustomer ID\t: " + CustID ); System.out.println("\tCustomer Name\t: " + CustName); System.out.println("\tCustomer Address\t: " + CustAddress); System.out.println("\tCustomer Contact\t: " + CustContact); System.out.println("\tCustomer Age\t: " + CustAge); System.out.println("\tCustomer Order Number\t: " + CustOrderNumber); System.out.println("\tCustomer Gender\t: " + CustGender); } else if (choice=="2") { System.out.println("======================================================"); System.out.println("| Order |"); System.out.println("======================================================"); System.out.println("Enter Customer ID"); CustID=br.readLine(); System.out.println("Enter Order Number"); OrderNo=br.readLine(); System.out.println("Enter Date"); DT=br.readLine(); System.out.println("Enter Menu Code"); MenuCode=br.read(); System.out.println("Enter Venue"); Venue=br.read(); System.out.println("Enter Quantity"); Quantity=br.read(); Payment=Venue+(Quantity*MenuCode); System.out.println("\t\tOrder Detail\t\t"); System.out.println("\tCustomer ID\t: " + CustID); System.out.println("\tDate\t\t: " + DT); System.out.println("\tMenu Code\t " + MenuCode ); System.out.println("\tVenue\t\t " + Venue); System.out.println("\tQuantity\t " + Quantity); } else if (choice=="3") { System.out.println("======================================================"); System.out.println("| Employee |"); System.out.println("======================================================"); System.out.println("Enter Employee ID"); EmpID=br.readLine(); System.out.println("Enter Employee Name"); EmpName=br.readLine(); System.out.println("Enter Employee IC"); EmpIC=br.readLine(); System.out.println("Enter Employee Address"); EmpAddress=br.readLine(); System.out.println("Enter Employee Position"); EmpPosition=br.readLine(); System.out.println("Enter Employee Gender"); EmpGender=br.readLine(); System.out.println("Enter Employee Status"); EmpStatus=br.readLine(); System.out.println("Enter Employee Hourly Rate"); EmpHourlyRate=br.readLine(); System.out.println("Employee Detail"); System.out.println("\tEmployee ID\t :" + EmpID); System.out.println("\tEmployee Name\t :" + EmpName); System.out.println("\tEmployee IC\t :" + EmpIC); System.out.println("\tEmployee Address\t :" + EmpAddress); System.out.println("\tEmployee Position\t :" + EmpPosition); System.out.println("\tEmployee Gender\t :" + EmpGender); System.out.println("\tEmployee Status\t :" + EmpStatus); System.out.println("\tEmployee Hourly Rate\t :" + EmpHourlyRate); } else if (choice=="4") { System.out.println("======================================================"); System.out.println("| Menu |"); System.out.println("======================================================"); System.out.println("\tMenu Code\t\tPrice"); System.out.println("\t10t\tRM 10"); System.out.println("\t20\tRM 20"); System.out.println("\t30t\tRM 30"); System.out.println("\t40t\tRM 40"); System.out.println("\t50\tRM 50"); } else if (choice=="5") { System.out.println("======================================================"); System.out.println("| Venue |"); System.out.println("======================================================"); System.out.println("\tVenue Code\t\tPrice\t\tLimit"); System.out.println("\t200 Zone\t\t200\t\t100"); System.out.println("\t400 Zone\t\t400\t\t150"); System.out.println("\t600 Zone\t\t600\t\t250"); System.out.println("\t800 Zone\t\t800\t\t375"); System.out.println("\t1000 Zone\t\t1000\t\t450"); } else if (choice=="6") { System.out.println("======================================================"); System.out.println("| Calculate Customer Payment |"); System.out.println("======================================================"); System.out.println("Enter Customer Name"); CustName=br.readLine(); System.out.println("Enter Venue Price"); VenuePrice=br.read(); System.out.println("Enter Menu Price"); MenuPrice=br.read(); System.out.println("Enter Number Of Sets"); NoSet=br.read(); Payment=VenuePrice+(MenuPrice*NoSet); System.out.println("Customer payment"); System.out.println("Customer Name : " + CustName); System.out.println("Payment : " + Payment); } else if (choice=="7") { System.out.println("======================================================"); System.out.println("| Calculate Employee Salary |"); System.out.println("======================================================"); System.out.println("Enter Employee name"); EmpName=br.readLine(); System.out.println("Enter Working hours"); WorkHour=br.read(); System.out.println("Enter Hourly Rate"); HourlyRate=br.read(); Salary=WorkHour*HourlyRate; System.out.println("Employee Salay"); System.out.println("Employee Name : " + EmpName); System.out.println("Employee Salary : " + Salary); } else System.out.println("Wrong Enter"); } }Last edited by Fubarable; 08-21-2009 at 05:47 PM. Reason: Fixed code tags
-
It's OK to use == when comparing primitives such as chars, but you shouldn't use it to compare Strings. Instead use the equals or equalsIgnoreCase methods.
i.e., change this
to this:Java Code:else if (choice=="4") {
Also please note that I fixed your code tags.Java Code:else if (choice.equals("4")) {
By the way, welcome to the forum!
- 08-21-2009, 05:57 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
yea!!!!!!!!!!!
can solve with that way!!!!
thx for helping me !!!
-
Cool. You're now well on your way to a fun and profitable career in computer programming.
- 09-29-2009, 07:31 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
Hello there,
I had the same problem with one of my program. Thanks to you Fubarable, I have been able to fix it. Cheers !
I would like to add another question to this topic. My program function pretty much like f4nt4sy one: I have a few System.out.println at the beginning (like a Menu) and according to the number you type you are directed to a IF condition. How could I go back to the beginning of the program at each end of the IF conditions, like a Goto thing.
I know, GOTO IS BAD (That's the result to googling "java + goto").
Here is my code:
package client;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class MyActionClient extends Client{
public static String readKeyboard(){
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(reader);
try
{
return keyboard.readLine();
}
catch (IOException e)
{
System.out.println("There was an error");
}
return "";
}
public static void main(String[] args) throws Exception{
connect("127.0.0.1",2002);
System.out.println("What do you want to do?");
System.out.println("Enter 1 if you want to Add an item");
System.out.println("Enter 2 if you want to Delete an item");
System.out.println("Enter 3 if you want to Bid on an item");
System.out.println("Enter 4 if you want to Browse the Auction List ");
String inp = readKeyboard();
if (inp.equals("1"))
{
System.out.println("You want to add an item.");
System.out.println("Please enter the name of your item, a description and a price:");
send("add");
String addname = readKeyboard();
send(addname);
String adddescription = readKeyboard();
send(adddescription);
String addprice = readKeyboard();
send(new Double(addprice));
System.out.println(receive());
}
else if (inp.equals("2"))
{
System.out.println("You want to delete an item.");
System.out.println("Please enter the number of the item you want to delete:");
send("del");
String delete = readKeyboard();
send(delete);
System.out.println(receive());
}
else if (inp.equals("3"))
{
System.out.println("You want to Bid on an item.");
System.out.println("Please enter the number of the item you would like to bid on, followed by your name and price");
send("bid");
String bidnumber = readKeyboard();
send(bidnumber);
String bider = readKeyboard();
send(bider);
String bidprice = readKeyboard();
send(new Double(bidprice));
System.out.println(receive());
}
else if (inp.equals("4"))
{
send("cat");
System.out.println(receive());
}
else if (inp.equals("5"))
{
System.exit(0);
}
else
System.out.println("Wrong Enter");
}
}
- 09-29-2009, 07:57 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You break down your program into separate functions. The menu part goes into a dispalyMenu() which you then simply call at the end of the if.
- 09-29-2009, 08:05 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
I'm really new to Java. Could you give me a simple example I could then adapt to my case ?
Thanks
- 09-29-2009, 08:18 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You do know how to create a method, right?
- 09-29-2009, 08:35 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
The thing is the main body of the program do not recognize inp anymore once the method is created.public static void displayMenu(){
System.out.println("What do you want to do?");
System.out.println("Enter 1 if you want to Add an item");
System.out.println("Enter 2 if you want to Delete an item");
System.out.println("Enter 3 if you want to Bid on an item");
System.out.println("Enter 4 if you want to Browse the Auction List ");
System.out.println("Enter 5 if you want to Exit");
String inp = readKeyboard();
}Last edited by Slaughterman; 09-29-2009 at 08:49 PM.
- 09-29-2009, 08:50 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
So call the method selectMenuOption instead and let it return the option selected.
You should be trying to figure out these things yourself.
- 09-29-2009, 10:39 PM #11
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
The thing is I have been ask to use readKeyboard() only for the entry.
Anyway I tried selectMenuOption and couldn't get it to work. :s
- 09-29-2009, 10:50 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Ah well, so don't add the String inp = readKeyboard(); line to the displayMenu method in the first place. It's not part of displaying a menu anyway.
- 09-30-2009, 10:09 PM #13
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
Ok, I found the solution, here is the code:
I have another problem with a part of a different program:package client;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class MyActionClient extends Client{
public static String readKeyboard(){
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(reader);
try
{
return keyboard.readLine();
}
catch (IOException e)
{
System.out.println("There was an error");
}
return "";
}
public static void main(String[] args) throws Exception{
char loop = ' ';
connect("127.0.0.1",2002);
do
{
System.out.println("What do you want to do?");
System.out.println("Enter 1 - Add an Item");
System.out.println("Enter 2 - Delete an Item");
System.out.println("Enter 3 - Bid on an item");
System.out.println("Enter 4 - Auction List");
System.out.println("Enter 5 - Exit");
String inp = readKeyboard();
if (inp.equals("1"))
{
System.out.println("You want to add an item.");
System.out.println("Please enter the name of your item, a description and a price:");
send("add");
String addname = readKeyboard();
send(addname);
String adddescription = readKeyboard();
send(adddescription);
String addprice = readKeyboard();
send(new Double(addprice));
System.out.println(receive());
loop='A';
}
else if (inp.equals("2"))
{
System.out.println("You want to delete an item.");
System.out.println("Please enter the number of the item you want to delete:");
send("del");
String delete = readKeyboard();
send(delete);
System.out.println(receive());
loop='A';
}
else if (inp.equals("3"))
{
System.out.println("You want to Bid on an item.");
System.out.println("Please enter the number of the item you would like to bid on, followed by your name and price");
send("bid");
String bidnumber = readKeyboard();
send(bidnumber);
String bider = readKeyboard();
send(bider);
String bidprice = readKeyboard();
send(new Double(bidprice));
System.out.println(receive());
loop='A';
}
else if (inp.equals("4"))
{
send("cat");
System.out.println(receive());
loop='A';
}
else if (inp.equals("5"))
{
System.out.println("Bye");
System.exit(0);
}
else
System.out.println("Wrong Enter");
loop='A';
}
while(loop=='A');
}
}
Basically, I want it to continue to accept Name until "bye" is entered.public static void main(String[] args) throws Exception{
connect("127.0.0.1",2002);
do
{
System.out.println("Enter your name:");
String inp = readKeyboard();
send(inp);
System.out.println(receive());
}
while(inp!="bye");
System.exit(0);
}
}
- 10-01-2009, 09:23 AM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Don't use == for comparing strings. Use the equals method instead.
- 10-01-2009, 10:19 AM #15
along with equals() use trim() also for removing the spaces before comparison using equals()
Ramya:cool:
- 10-01-2009, 12:09 PM #16
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
The problem is the while condition don't recognize the inp, it says it cannot be resolved. If I had a "String inp2 = readKeyboard()" outside the do, it recognize it but the code is broken because there is something between the end of the do and the while condition.
:/
- 10-01-2009, 12:22 PM #17
i'm not a fan of the endless else if statements. i prefer using a switch case wich makes your program more readable and it's easier to use.
note; it has the same functions as an else if but it looks nices in your code and better for others to understand it :)
all the best,
DieterProgramming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...
- 10-01-2009, 12:34 PM #18
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-01-2009, 12:41 PM #19
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
Yes, I figured that. But the whole problematic is to make the program stop (or go out of the loop if you want) when bye is entered. If I declare the inp before the do, the one in the do is broken (duplicate local variable), and I can't make another one (let's say inp2) because the checking part would be useless since the one I need to check before going back or not to the beginning of the program is really the "inp" in the do.
Maybe I need to change the mechanism and forget about do/while...
Any suggestion ?
- 10-01-2009, 12:47 PM #20
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
[newbie]
By jon80 in forum New To JavaReplies: 10Last Post: 07-07-2009, 12:10 PM -
Newbie Help
By mattkid in forum New To JavaReplies: 4Last Post: 03-25-2009, 04:55 AM -
newbie newbie newbie
By krislogy in forum New To JavaReplies: 9Last Post: 08-15-2008, 12:28 AM -
newbie needs help...
By vicky08 in forum New To JavaReplies: 2Last Post: 03-31-2008, 04:26 PM -
Newbie
By CSnoob87 in forum IntroductionsReplies: 2Last Post: 02-18-2008, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks