
-----------------------------------
MysticVegeta
Sun Sep 04, 2005 12:10 pm

Java Strings whirlpool... oh my god!
-----------------------------------
Hello,
I decided to learn Java over the summer, and so i did, although the strings are really giving me some problems. I got the IO Part, I got the Ints, but the strings... damn
ok, now,

int foo;
foo = System.in.read();
System.out.println(foo);

Can someone tell me why is it that this returns 49 when i input in 20?

Also

String fi;
fi = System.in.read();


Why does this give me an error?

Apparently i thought this was going to be easier than for loops and arrays. but i was wrong, i learned them before knowing how to get a string variable. lol. help?

-----------------------------------
wtd
Sun Sep 04, 2005 12:56 pm


-----------------------------------
I think maybe the "read" method isn't doing what you think it's doing?

What are you trying to accomplish?  If you just want to read in a String, you should wrap System.in in a BufferedReader instance.

-----------------------------------
Aziz
Sun Sep 04, 2005 9:33 pm


-----------------------------------
If he's the experience level I think he is, I think he doesn't know what that means :P Try the Java Tutorial, here: http://java.sun.com/docs/books/tutorial

-----------------------------------
MysticVegeta
Mon Sep 05, 2005 9:50 am


-----------------------------------
ok what i am trying to do is "get" a string
var str : string
get str
put str


Also

var integer : int
get integer
put integer


Why, if i input 20 as an integer, it outputs 49?

-----------------------------------
Aziz
Mon Sep 05, 2005 10:30 am


-----------------------------------
The System.in.read() method is not for that, I believe.

What you want to do is:

[syntax"java"]BufferedReader in = new BufferedReader(System.in);

String myString = in.readLine();[/syntax]

Let me explain. As much as I know. rizzix or wtd would be better at this, though, but I'll just let you know a least a little bit of whats going on.

BufferedReader is a stream reader. It reads data. We're "creating" (instantiate, i think is the proper word) a BufferedReader object here. When you create a BufferedReader here, we're giving it System.in. System.in is a stream coming from the keyboard (I believe, maybe from somewhere else?) Just like System.out is a stream. When you call System.out.println("hello"), you're telling the program to send the text "hello" through the System.out stream, which goes to the console output. Now readLine(); is a method (function in Turing terms) that returns a String. So you tell your reader (we've named it 'in') to read a line ('readLine()') from it's stream ('System.in') get it? If not, I'm sure the 1337 rizzix or wtd will help you :P (but hopefully not before the help me :( )

-----------------------------------
1of42
Mon Sep 05, 2005 12:06 pm


-----------------------------------
Yeah, that's basically correct. vegeta, given that I've never used the read() function, I can't help you with that, but the following lines will get an integer from the console:


int number;
BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));

try
{
   number = Integer.parseInt (reader.readLine ().trim ());
}
catch (NumberFormatException ex)
{
   System.out.println ("Sorry, that was not a valid number");
}


-----------------------------------
[Gandalf]
Mon Sep 05, 2005 1:28 pm


-----------------------------------
import java.io;

public class Prog
{
	public static void GetName ()
	{
		String name;
		
		BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
		System.out.println("Enter your name: ");
		name = reader.readLine();
	}
	
	public static void main (String 

---------- Capture Output ----------
> "C:\j2sdk1.4.2_06\bin\javac.exe" Prog.java
Prog.java:1: cannot resolve symbol
symbol  : class io 
location: package java
import java.io;
            ^
Prog.java:9: cannot resolve symbol
symbol  : class BufferedReader 
location: class Prog
		BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
                ^
Prog.java:9: cannot resolve symbol
symbol  : class BufferedReader 
location: class Prog
		BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
                                            ^
Prog.java:9: cannot resolve symbol
symbol  : class InputStreamReader 
location: class Prog
		BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
                                                                ^
4 errors
> Terminated with exit code 1.

-----------------------------------
rizzix
Mon Sep 05, 2005 2:55 pm


-----------------------------------
it should be import java.io.*;... you import the classes.. not the package..

-----------------------------------
wtd
Mon Sep 05, 2005 3:14 pm


-----------------------------------
"]import java.io;

public class Prog
{
	public static void GetName ()
	{
		String name;
		
		BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
		System.out.println("Enter your name: ");
		name = reader.readLine();
	}
	
	public static void main (String 

As rizzix pointed out, your import is a bit goofy, and not at all valid.  ;)

Also, method names in Java should always begin with a lower-case letter.

-----------------------------------
[Gandalf]
Mon Sep 05, 2005 3:36 pm


-----------------------------------
As rizzix pointed out, your import is a bit goofy, and not at all valid.  ;)
Yes... :doh:  I was trying to do it from memory, I was bound to make mistakes.

Also, method names in Java should always begin with a lower-case letter. 
Yes, it's a habit, I usually have variables like that.  You say that variables and methods should be this way?  I'm used to thinking of the two quite differently.

---------- Capture Output ----------
> "C:\j2sdk1.4.2_06\bin\javac.exe" Prog.java
Prog.java:11: unreported exception java.io.IOException; must be caught or declared to be thrown
		name = reader.readLine();
Must I learn up on throwing now, or am I doing something wrong?

-----------------------------------
wtd
Mon Sep 05, 2005 3:41 pm


-----------------------------------
"]Also, method names in Java should always begin with a lower-case letter. 
Yes, it's a habit, I usually have variables like that.  You say that variables and methods should be this way?  I'm used to thinking of the two quite differently.---------- Capture Output ----------
> "C:\j2sdk1.4.2_06\bin\javac.exe" Prog.java
Prog.java:11: unreported exception java.io.IOException; must be caught or declared to be thrown
		name = reader.readLine();
Must I learn up on throwing now, or am I doing something wrong?public static void main(String

-----------------------------------
MysticVegeta
Tue Sep 06, 2005 1:35 pm


-----------------------------------
thanks a lot for the help guys :) I have used the bufferedreader before when doing the IO. Thanks a lot :D

-----------------------------------

Tue Sep 06, 2005 8:42 pm


-----------------------------------
Concerning your first implementation. When you input 20, are you sure it shows 49, not 50? Because System.in.read() reads one character. If you input 20, it will read the first character '2', and store it as an integer. Casting occures automaticaly in this case. So when you print the integer, it is equivalent to printing (int)'2', which is 50.
