The Artima Developer Community
Sponsored Link

Java Answers Forum
How To Get The Correct Outcome

6 replies on 1 page. Most recent reply: Feb 16, 2004 4:19 AM by Kevin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How To Get The Correct Outcome Posted: Feb 15, 2004 5:46 PM
Reply to this message Reply
Advertisement
I have written below code for my program but i encounter problem in the If/Else code...
I am not able to find out what wrong with my code...
My expected outcome should work something like this...
When i prompt the user to input their selection, the user will choose either S , D , C , E.
Then, the If/Elsewill determine the user input and print out the correct selection.
However, the outcome always print out Wrong otion!.

The code is as below:

import java.io.*;

public class Choice
{public static void main(String[] args) throws IOException
{InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print(">Option: ");
String text = input.readLine();
System.out.println("Your choice: " + text);

if(text == "S")
System.out.println("Sales");
else if (text == "D")
System.out.println("Daily Order");
else if (text == "C")
System.out.println("Collection");
else if (text == "E")
System.out.println("Exit");
else
System.out.println("Wrong option!");
}
}

Thanks for your help in advance...


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How To Get The Correct Outcome Posted: Feb 15, 2004 6:05 PM
Reply to this message Reply
Use equals() method of String class to check if user input is same as "S", "C" or other choices and not ==.
import java.io.*;
 
public class Choice {
	public static void main(String[] args) throws IOException	{
		InputStreamReader reader = new InputStreamReader(System.in);
		BufferedReader input = new BufferedReader(reader);
		System.out.print(">Option: ");
		String text = input.readLine();
		System.out.println("Your choice: \"" + text + "\"");
 
		if(text.equals("S"))
			System.out.println("Sales");
		else if (text.equals("D"))
			System.out.println("Daily Order");
		else if (text.equals("C"))
			System.out.println("Collection");
		else if (text.equals("E"))
			System.out.println("Exit");
		else
			System.out.println("Wrong option!");
	}
}

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How To Get The Correct Outcome Posted: Feb 15, 2004 6:23 PM
Reply to this message Reply
Thank you so much, Kishori.
U have offered a big help for me.

Can i do something to remove the ""in the program output?

Your choice: "S"

And can u guys tell me how to add method in the IF/ELSE option, for example, when S is detected in the IF/ELSE, a method that do the sales calculation is performed. When D is detected, the Daily Order calculation is performed and if E is selected, then the program will terminated and exit...

For the time being, i just need to know framework of code how to write.
I'll try to write the code...

Thanks...

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How To Get The Correct Outcome Posted: Feb 15, 2004 8:23 PM
Reply to this message Reply
My first suggestion is to read the documentation on the String class. You will find many of the answers to your questions there. In it, you will find the equals method, suggested in the previous post. You will also find the following, which I copied from the documentation.

boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.

int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.

String toLowerCase()
Converts all of the characters in this String to lower case

String toUpperCase()
Converts all of the characters in this String to upper case

All of these might be useful for what you are trying to do. You can find the documentation on the web, or you can download it to your computer. It is worth the download time.

I hope this helps.

Zafar Iqbal Uppal

Posts: 1
Nickname: uppal62
Registered: Mar, 2003

Re: How To Get The Correct Outcome Posted: Feb 15, 2004 10:14 PM
Reply to this message Reply
you must use text.equals("S") in the if statement.

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How To Get The Correct Outcome Posted: Feb 15, 2004 11:54 PM
Reply to this message Reply
Thanks guys.

I just want to check with u guys one more question.
My question is that can i add a method or a function to each if/else so that each selection will perform certain operation.

Thanks a lot...

Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Re: How To Get The Correct Outcome Posted: Feb 16, 2004 4:19 AM
Reply to this message Reply
YOu can also use
 if (text.compareTo("S")==0) 
// instead of using
if (text.equals("S")) 

Flat View: This topic has 6 replies on 1 page
Topic: How To Get User Input from Command Prompt Previous Topic   Next Topic Topic: Compiler vs. Run-Time Exceptions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use