This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help - if else - command line

 
Michael Calder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

It should be command.equals("del") instead of command == "del". Same for the rest.

Joyce
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change if (command == "del") {
to
if (command.equals("del")) {
nd the like and all will be well
 
Michael Calder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic