Hi all - for my beginner java class, I'm writing a program that accepts user input to determine a math operation, does the math, and then tells the user if they are right or wrong. I think I've got all of the necessary elements included, but I'm having trouble writing the code to accept the user input and do the correct operation based on that. Below is the code i already have: we are working in BlueJ and it compiles and runs, just not properly. (I had a bunch of other code in there as comments that i deleted when I posted it here, so if it's missing brackets, it could be because I accidentally grabbed them in the delete).
Does anyone have any suggestions?
(edit - I don't think i was clear with my problem.. the program "Accepts" user input, but it doesn't seem to apply the input to the loops)
1. try to use code tags when you post. It makes the posting MUCH easier to read. After you paste your code in, and while it is still "selected" hit the "code button".
2. When you say "you are having trouble with user input" can you be more specific?
3. I think it is most common/easiest to use the Scanner class to read from keyboard. You are using InputReader, which is a not a part of the standard Java API....Do you have documentation and/or examples for it?
Joined: Apr 17, 2010
Thanks for the tip - I went back and added the code tag.
When i say trouble with user input, i meant that the program will accept input when the run method is invoked, but that..(I'm sorry, I don't know the proper words) there is a disconnect between the user input and the rest of the program.
IE - I wrote the rest of the code to "do" things with the input, but it doesn't. It recognizes that input has been given, since it proceeds to the next line, but it doesn't take the information and process it. I'm asking it to take 1, 2 or 3 and do an operation based on that - it "kind" of does - but seemingly randomly.
Does that make sense? My apologies, I'm truly foundering with this language.
Also, my professor had requested that we use InputReader based on examples she provided. I will post the documentation.
public class InputReaderextends java.lang.Object InputReader reads typed text input from the standard text terminal. The text typed by a user is then chopped into words, and a set of words is provided.
Michael Kolling and David J. Barnes
InputReader() Create a new InputReader that reads text from the text terminal.
getInput() Read a line of text from standard input (the text terminal), and return it as a String.
Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Create a new InputReader that reads text from the text terminal.
public java.lang.String getInput()
Read a line of text from standard input (the text terminal), and return it as a String.
A String typed by the user.
Joined: Oct 28, 2009
Ok...you're moving forward....but your code still needs some serious first aid.
A couple things:
Your logic on your if/else if blocks looks faulty. You want to do some set of things if the user enters a "2"...so why are you using the not "!" operator?
maybe something like
also, you are re-declaring your variable userChoiceAsString inside of each logical segment, so that it is only visible inside that code block. I don't think you want to do that.
and, to convert your strings to Integers, easiest way is:
you will need to do something like that when you get your user's guess.
and as a simple troubleshooting tool, put some print statements in and then comment them out later such as:
System.out.println("evaluating the addition portion!");
Joined: Apr 17, 2010
Thanks so much. The parsing instructions worked well and removing the ! operator eliminated the wackiness I was having.
Now, the program runs, requests input for the name, generates the random number, and also requests the user's choice.. and then selects an operation and does a line print based on that choice! You have NO idea what a triumph this is for me.
Now I'm working on creating a loop that will compare the user's input for an answer to the actual answer... I'm struggling with it, but I feel like I'm almost there. Thanks so much for the help!