Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    f4nt4sy is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    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
    Java Code:
    else if (choice=="4")
    {
    to this:
    Java Code:
    else if (choice.equals("4"))
    {
    Also please note that I fixed your code tags.

    By the way, welcome to the forum!

  3. #3
    f4nt4sy is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default

    yea!!!!!!!!!!!
    can solve with that way!!!!
    thx for helping me !!!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

  5. #5
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    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");

    }
    }

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    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.

  7. #7
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    I'm really new to Java. Could you give me a simple example I could then adapt to my case ?

    Thanks

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You do know how to create a method, right?

  9. #9
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    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();

    }
    The thing is the main body of the program do not recognize inp anymore once the method is created.
    Last edited by Slaughterman; 09-29-2009 at 08:49 PM.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    So call the method selectMenuOption instead and let it return the option selected.
    You should be trying to figure out these things yourself.

  11. #11
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    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

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    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.

  13. #13
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    Ok, I found the solution, here is the 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{

    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');
    }
    }
    I have another problem with a part of a different program:

    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);
    }
    }
    Basically, I want it to continue to accept Name until "bye" is entered.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Don't use == for comparing strings. Use the equals method instead.

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    877
    Rep Power
    8

    Default

    along with equals() use trim() also for removing the spaces before comparison using equals()
    Ramya:cool:

  16. #16
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    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.

    :/

  17. #17
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    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,

    Dieter
    Programming 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...

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Quote Originally Posted by Slaughterman View Post
    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.

    :/
    Of course it's not recognized. It's declared inside the do block. That's basic Java scoping rules doing their job. Declare it before the do block.

  19. #19
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    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 ?

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    No you need to think about these things a bit more. That's how you learn programming.
    If you declare the inp before the do, you don't need to declare a new one inside the do.
    Just use the same one and change the value inside the do.

Page 1 of 2 12 LastLast

Similar Threads

  1. [newbie]
    By jon80 in forum New To Java
    Replies: 10
    Last Post: 07-07-2009, 12:10 PM
  2. Newbie Help
    By mattkid in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 05:55 AM
  3. newbie newbie newbie
    By krislogy in forum New To Java
    Replies: 9
    Last Post: 08-15-2008, 12:28 AM
  4. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 04:26 PM
  5. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •