• Post Reply Bookmark Topic Watch Topic
  • New Topic

I think it is a scanner issue...  RSS feed

 
Jason Newman
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... But I'm not exactly sure. I know that the scanner scans input and then determines what to do. So, I have set up an if statement to.... You know what, the code speaks louder than words.


So basically I'm trying to make my eight ball program ( original: http://www.mediafire.com/?kd4ldjdxh3j2n01) better by it asking you if you want to make a wish or not. Then the if statement sets the variable of test to 1 for yes, 2 for no. Yet, it won't work. Please help?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, the syntax for this line
if(blarg == "yes" || "Yes")
is wrong. If you are going to user logical operators, the statement on each side of the operator must be complete and capable of standing alone. So it would be
if(blarg == "yes" || blarg == "Yes")

Having said that, you don't use == to compare Strings. You use the equals method, so that line should be
if(blarg.equals("yes") || blarg.equals("Yes"))

And finally, if you look at the javadoc for the String class, you should be able to find a method which will allow you to do this with just one call, so you don't need the || operator.
 
Siddhesh Deodhar
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(blarg == "yes" || blarg == "Yes")


or you can use [whatever Joanne meant]

Scanner will take input from console that you have provided.
What other problem you are facing?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddhesh Deodhar, please look at the Beginning Java™ page, where you find this:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Joanne had given enough of a hint for JN to find that method, so by giving too much of an answer you may cause him to miss an opportunity to learn. I have pulled rank and changed your post, so JN will get the full benefit of Joanne's help. Please avoid giving too much of an answer too quickly.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddesh Deodhar has apologised, so I shall say . . .

Apology accepted
 
Jason Newman
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I changed the ode around and it will compile, with the change to the if || statment that the other JN gave me. But, the problem now is that it will show up in the pane with "Do you want to make a wish?" and if you type yes || no it freaks out and pritns an empty imput box into the console. If you type in it,... Well... java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at EightBallTEST.main(EightBallTEST.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)


I think we may have a problem.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you mixing Scanner and JOptionPane? You can read text with the methods of Scanner too.
Try a few lines with Scanner, getting ints and Strings in turn, and see how it works. Beware that Scanner can appear to miss out an entry if you have a return. Look at this old post, complete with spelling errors.
 
Jason Newman
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you mixing Scanner and JOptionPane? You can read text with the methods of Scanner too.
Try a few lines with Scanner, getting ints and Strings in turn, and see how it works. Beware that Scanner can appear to miss out an entry if you have a return. Look at this old post, complete with spelling errors.


What would you recommend that I display this eight ball on if not the JOptionPane? And I still can't get it to run right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!