0

I'm just wondering if it would be possible to read in a String value with a Scanner,convert it to upper case, and use .equals("NO") in the same if statement. Basically, if the user enters 'no' 'No' 'NO' 'nO' it will change it to 'NO' and then perform .equals("NO") to check if the user wants to continue the program. Yes, I'm aware that the current syntax of the if statement parameters are incorrect, this is because I'm not sure how to do it. Would I be able to do toUpperCase and equals() in the if statement, or would I need to use a seperate toUpperCase statement?

right after public class program2 {

public static String cont="";
public static Scanner in=new Scanner(System.in);

within my main();

System.out.print("Continue? Yes/No >> ");
cont=in.nextLine();
if(cont.toUpperCase/equals("NO")
{
System.exit(0);
}
showmenu();

Additionally, since I'm using Syste.out.print() and System.out.println(), it would be helpful if there was a way to clear all text from the output window. Is there a way to clear the output window?

A.H.
  • 63,967
  • 15
  • 92
  • 126
Brandon Durst
  • 71
  • 1
  • 4
  • 12
  • 1
    `cont.toUpperCase()` returns a String, so `cont.toUpperCase().equals("NO")` would call `equals` on the string returned by `toUpperCase()`. In other words, yes, you can put both methods in the same if statement. The only time you can't chain methods like this is if the first method is a void method, in which case there's no object for the next method to call on. – Zim-Zam O'Pootertoot Apr 17 '13 at 17:34
  • http://stackoverflow.com/questions/7522022/how-to-delete-stuff-printed-to-console-by-system-out-println – Bhavik Shah Apr 17 '13 at 17:37
  • 1
    @Brandon: Please don't squeeze independent stuff into one question. Other people profit more from this site if they can find answers to theirs problems by meaningful titles and tags. – A.H. Apr 17 '13 at 17:56

4 Answers4

10

Use String.equalsIgnoreCase() to test for equality ignoring case.

7

Yes you can do this. Just do this:

if (s.toUpperCase().equals("NO")) ...
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    One note related note, do not do s.toUpperCase(), then do s.equals("NO"). toUpperCase() returns a new String representing s upper cased, it does not actually change s to upper case unless you reassign it. This is why this method chain works as @tieTYT stated, it calls toUpperCase() on s, which returns a new String, the new string is then used in the equals("NO") expression. This way, s is maintained, and you get the desired result. – dardo Apr 17 '13 at 17:37
1

You can do that

cont.toUpperCase().equals("NO")
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
1

1)You can do this :

// include a null check here
if (cont!=null && cont.toUpperCase().equals("NO")) {}

OR

if(cont.equalsIgnoreCase("No"))

2) To clear the console :

Runtime.getRuntime().exec("cls"); // cls command is OS dependent
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • I'm getting the error "unreported exception java.io.IOException; must be caught or declared to be thrown" even after I added "import java.io.*;" and changed "public static void main(String[] args)" to "public static void main(String[] args)throws IOException" – Brandon Durst Apr 17 '13 at 17:47
  • You shouldn't get that of you have added the throws clause in main() – AllTooSir Apr 17 '13 at 17:50
  • Here's my complete code as of now. I've inserted /*---------*/ above each line I just entered, which was an import, the RunTime, and main() http://pastebin.com/HJjY1Z6e – Brandon Durst Apr 17 '13 at 18:05