Computer Science Canada

Java Strings whirlpool... oh my god!

Author:  MysticVegeta [ Sun Sep 04, 2005 12:10 pm ]
Post subject:  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,

code:
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

code:
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?

Author:  wtd [ Sun Sep 04, 2005 12:56 pm ]
Post subject: 

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.

Author:  Aziz [ Sun Sep 04, 2005 9:33 pm ]
Post subject: 

If he's the experience level I think he is, I think he doesn't know what that means Razz Try the Java Tutorial, here: http://java.sun.com/docs/books/tutorial

Author:  MysticVegeta [ Mon Sep 05, 2005 9:50 am ]
Post subject: 

ok what i am trying to do is "get" a string
Turing:
var str : string
get str
put str


Also

Turing:
var integer : int
get integer
put integer


Why, if i input 20 as an integer, it outputs 49?

Author:  Aziz [ Mon Sep 05, 2005 10:30 am ]
Post subject: 

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 Razz (but hopefully not before the help me Sad )

Author:  1of42 [ Mon Sep 05, 2005 12:06 pm ]
Post subject: 

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:

Java:

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");
}

Author:  [Gandalf] [ Mon Sep 05, 2005 1:28 pm ]
Post subject: 

Java:
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 [] args)
        {
                System.out.println("Hello person!");
                GetName();
        }
}


Quote:
---------- 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.

Author:  rizzix [ Mon Sep 05, 2005 2:55 pm ]
Post subject: 

it should be
Java:
import java.io.*;
... you import the classes.. not the package..

Author:  wtd [ Mon Sep 05, 2005 3:14 pm ]
Post subject: 

[Gandalf] wrote:
Java:
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 [] args)
        {
                System.out.println("Hello person!");
                GetName();
        }
}


As rizzix pointed out, your import is a bit goofy, and not at all valid. Wink

Also, method names in Java should always begin with a lower-case letter.

Author:  [Gandalf] [ Mon Sep 05, 2005 3:36 pm ]
Post subject: 

wtd wrote:
As rizzix pointed out, your import is a bit goofy, and not at all valid. Wink

Yes... Doh! I was trying to do it from memory, I was bound to make mistakes.

wtd wrote:
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.

Quote:
---------- 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?

Author:  wtd [ Mon Sep 05, 2005 3:41 pm ]
Post subject: 

[Gandalf] wrote:
wtd wrote:
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.
[/quote]

Yes, both should use that naming convention.

Quote:
---------- 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?[/quote]

Exceptions in Java are checked. You either have to explicitly catch the exception in your method, or report that the method is capable of throwing that exception.

Java:
public static void main(String[] args) throws IOException

Author:  MysticVegeta [ Tue Sep 06, 2005 1:35 pm ]
Post subject: 

thanks a lot for the help guys Smile I have used the bufferedreader before when doing the IO. Thanks a lot Very Happy

Author:  Anonymous [ Tue Sep 06, 2005 8:42 pm ]
Post subject: 

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.


: