• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loops and boolean operators

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Thanks!
Christine

(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)

/
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Christine.

A couple points/questions:

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?

 
Christine Marie
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,

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.

edit: documentation for inputReader



Class InputReader
java.lang.Object
InputReader



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.


Version:
0.1
Author:
Michael Kolling and David J. Barnes


Constructor Summary
InputReader()           Create a new InputReader that reads text from the text terminal.
 
Method Summary
 java.lang.String
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
 

Constructor Detail

InputReader
public InputReader()
Create a new InputReader that reads text from the text terminal.


Method Detail

getInput
public java.lang.String getInput()
Read a line of text from standard input (the text terminal), and return it as a String.

Returns:
A String typed by the user.
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!");

 
Christine Marie
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,

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!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The entire loop that you need, just input the code that would do that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic