| Author |
Help - if else - command line
|
Michael Calder
Greenhorn
Joined: Apr 14, 2004
Posts: 6
|
|
The code below allows the user to type in a command for a server, in the command line prompt. However I am having trouble with the if else statement. If the user types in a command (add, bid or del) it should do that bit of code, if they type something different it should say "Please enter a proper command". The problem is i type in the proper command and it is not valid, it always goes to "Please enter a proper command". Thanks in advance Mike import simpleDist.*; import java.io.*; public class myAuctionClient2 extends Client { public static void main(String[] args) throws Exception { String command = ""; String itemNo = ""; //Connect to your server connect("127.0.0.1", 1001); //Get the catalogue of articles for auction from the server send("cat"); System.out.println(recieve()); // Prompts the user to enter in a command add, bid or del System.out.print("Use add bid or del - "); InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); // read in user input try { command = reader.readLine(); } catch (Exception e) {} // Executes the command if (command == "del") { System.out.print("What is the item ID - "); try { itemNo = reader.readLine(); } catch (Exception e) {} send(command); send(itemNo); System.out.println(recieve()); } else if (command == "add") { System.out.println("Print something"); } else if (command == "bid") { System.out.println("Print something"); } else { System.out.println("Plese enter a proper command"); } send("cat"); System.out.println(recieve()); //Exit from the system. System.exit(0); } }
|
 |
Joyce Lee
Ranch Hand
Joined: Jul 11, 2003
Posts: 1392
|
|
Hi Michael, It should be command.equals("del") instead of command == "del". Same for the rest. Joyce
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
Change if (command == "del") { to if (command.equals("del")) { nd the like and all will be well
|
Mike Gershman
SCJP 1.4, SCWCD in process
|
 |
Michael Calder
Greenhorn
Joined: Apr 14, 2004
Posts: 6
|
|
|
Thank you!
|
 |
 |
|
|
subject: Help - if else - command line
|
|
|