wood burning stoves*
The moose likes Java in General and the fly likes newbie needs help with code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "newbie needs help with code" Watch "newbie needs help with code" New topic
Author

newbie needs help with code

MikeB
Greenhorn

Joined: Dec 11, 1999
Posts: 11
Hi All,
I'm just learning Java and have written this simple program and need help in understanding while the output for the numbers in the range that should print out retiree,child, and teenager all print out, "You are an adult." I understand why when I type in symbols or letters I get, "You
are a retiree."
Thanks for your help.
Here's the code:
import java.io.*;
public class Mike {
public static void main(String[] args) {
System.out.println("Type your age here ");
int a = 0;
try {
a = (int)System.in.read(); }
catch(IOException e) {
System.out.println(a + "\t"); }
if (a < 13)
System.out.println ("You are a child.");
else if (a < 18)
System.out.println("You are a teenager.");
else if (a < 65)
System.out.println("You are an adult.");
else
System.out.println("You are a retiree.");
}
}
Mike B.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

System.in.read() reads in the next byte of information. When you type a number, you get back an ascii value. When you type in the number 1, it has an ascii value of 49. 49 is an adult in your book.
Check out the class InputStream at http://java.sun.com/products/jdk/1.2/docs/api/index.html


permaculture Wood Burning Stoves 2.0 - 4-DVD set
MikeB
Greenhorn

Joined: Dec 11, 1999
Posts: 11
Thanks Sheriff Wheaton for replying. I don't understand your answer but will study it and check the link you posted. What I understand is somehow when I type in 1 it becomes something other than what I wanted it to be.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

Ahhh, the magic of ASCII.
Whelp, computers think only in ones and zeros, right? So what happens if you want it to think about the letter "A"??
A byte on a computer can hold a number from 10to 255. ASCII has defined meaning for the values 0 to 127. If you set your byte to 65, that means 'A'. 66 is 'B', 67 is 'C', etc. All the symbols, numbers and lower case letters are there too. Including some special characters that do things like mark the end of a line of text.
You told java that you wanted to read in the next byte. It did! 48 is the ascii value for the character '0'. So when your user types '0', the number you get is 48.
What you really want to do is read in a whole line of text and translate that into a meaninful integer.
I hope this helps.
MikeB
Greenhorn

Joined: Dec 11, 1999
Posts: 11
Thanks. I'm starting to get it very
sloooooooooooowly. Java is my first attempt to learn a programming language since 1979 when I learned COBOL, RPG II, and IBM Basic Assembler at Maxwell Institute in Norristown,
PA. This is oh so much different.
How would you change the code to do what you say needs to be done?
Thanks again,
Mike B.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

Me?
Well, I've always avoided the terminal input stuff in Java. When I need program input from the console, I always use the command line parameters ( String[] args ) in main.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Terminal input in Java is not as simple as for many other languages, unfortunately. There are a lot of different ways to do it - if you're using a GUI, you may well have a TextField that the user enters info in, and you get it with something like field.getText(). But for a reasonable approximation of good old-fashioned terminal input, try this:
<code><pre>
import java.io.*;
public class Test {
public static void main(String as[]){
System.out.println("Type your age here ");
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String response = br.readLine();
br.close();
isr.close();
int a = Integer.parseInt(response);
if (a < 13) {
System.out.println ("You are a child.");
} else if (a < 18) {
System.out.println("You are a teenager.");
} else if (a < 65) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a retiree.");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
</pre><code>
I kept the exception handling to a minimum - it can get much more involved if you want the program to be really robust, but that's not necessary here. If you're doing multiple inputs, you probably want to keep the streams open until you're done with all inputs - I just did the closes right away for simplicity.
There's quite a bewildering array of ways you can put together the various kinds of IO Streams - spend some time reading up on them, and trying things out.

[This message has been edited by Jim Yingst (edited January 31, 2000).]


"I'm not back." - Bill Harding, Twister
MikeB
Greenhorn

Joined: Dec 11, 1999
Posts: 11
Hey Jim, Thanks.
I'll look at the code tommorrow. That's what I wanted was
to see some code that would do the job and then try to
learn from that. I'm so new to Java that I don't understand
most explanations given to me but at least with code I can look
at it, compile it, play around with it. Thanks for continuing
my education.
Mike B.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newbie needs help with code