Java IO
Author |
Message |
S_Grimm

|
Posted: Wed Oct 15, 2008 2:39 pm Post subject: Java IO |
|
|
Ok, I just finished reading the I\O tutorials in the Tutorial Section in java AND searched the wiki, but they both were about as clear as mud. I understand the import for strings, but i need to get int double, byte, short, or long into a variable that i can manipulate. For example
Java: |
readInt (variable1 );
variable2 = Math. pow(variable1, 2);
System. out. println (variable2 );
|
So I guess what i need is this : How do i read int, double, long, etc... and store it in a variable?
Thanks in Advance
A\V |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
btiffin

|
Posted: Wed Oct 15, 2008 2:52 pm Post subject: Re: Java IO |
|
|
There are multiple ways; here is one sampler
code: | String str = "42";
int i = Integer.parseInt(str);
long l = Long.parseLong(str);
String snum = "42.123";
float f = Float.parseFloat(snum);
String sback = Float.toString(f);
| Conversion errors can be trapped with judicious use of try/catch.
Hope that helps.
Cheers |
|
|
|
|
 |
S_Grimm

|
Posted: Thu Oct 16, 2008 7:42 am Post subject: Re: Java IO |
|
|
no, i sort of need a Java: | Double variable1
//this code is going to be wrong, but it will show you what i want.
System. in (variable1 )
|
|
|
|
|
|
 |
wtd
|
Posted: Thu Oct 16, 2008 8:10 am Post subject: RE:Java IO |
|
|
Let me ask you... can you use System.in to read in a String? |
|
|
|
|
 |
[Gandalf]

|
Posted: Thu Oct 16, 2008 9:04 am Post subject: RE:Java IO |
|
|
There is a nifty Scanner class in Java 1.5 and up that makes console input much nicer, Google it. It will also save you the String cast. |
|
|
|
|
 |
S_Grimm

|
Posted: Thu Oct 16, 2008 3:02 pm Post subject: RE:Java IO |
|
|
yes. if you call it through BufferedReader
edit. alright. i got it to read an int. now is the time when we say how stupid i am and that biffin was right. i am a complete dumb***. sorry everybody...
now on to my next problem. I was trying to write a method that would allow me to call my own function to read int, but hit a snag. i'll post the code first. then go over my problem.
now, if you take the "num = " out of the in.readInt(); and comment the int num; and System.out.println (num); , the code works fine. what i want to do is be able to have num = in.readInt(); and return a value. can anyone give me a hand with this?
edit2:
never mind. i had to stick a return and change it from void to int. Ill post the code just incase anyone else whats to look at it and try to suggest something else...
|
|
|
|
|
 |
|
|