• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to have a fix System.out.print answer for any String  RSS feed

 
Andrew Concilio
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
I am pretty sure that I am not the first person asking this question but I am still confuse on how to come out with the answer, so here we go:
I am working on doing a code where I will be asking the user a question and depending on the answer I will print the statement let me more specific.

String answer1;
String answer2;
Scanner scan = new Scanner (System.in);
{ System.out.println("are you a Yankee fan?"); }
answer1 = scan.nextLine();

if (answer1.equals("yes") || answer.equals("y") || answer1.equals("Yes"))
System.out.println("Great" );
System.out.println('What do you like about the Yankees?");

answer2 = scan.nextLine();


this is where I get confuse how do I write
if ( answer2 equals any sentence )System.out.println("good for you");


in other words I don't care what type of sentence they write the answer is always going to be "good for you"

Thank you guys
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just write the print statement without any if-condition.
 
Andrew Concilio
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean like that

answer2 = scan.nextLine();
System.out.println("good for you");
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Andrew Concilio
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works! thank you Bear
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
 
Vinayak Deshpande
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Andrew,
instead of using nextLine(), you try next()
Like this:
Scanner scann=new Scanner(System.in);
String string=scan.next();
System.out.println("Good");

what this will do is you can check whether the user enters something or just leaves a blank space or fills the area
with white spaces it will return the control when the user enters some character other than white space or new line..

otherwise even if you give some positive msg even if the user doesn't input anything it looks awkward
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!