Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java IO
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
S_Grimm




PostPosted: 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
Sponsor
sponsor
btiffin




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




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




PostPosted: 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]




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




PostPosted: 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... Embarassed

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

import java.io.*;
public class read
{
public static void main (String [] args) throws IOException
{
int num;
read in = new read();
num = in.readInt();
System.out.println (num);
}
public void readInt() throws java.io.IOException
{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader (input);
int Int;
Int = Integer.parseInt(reader.readLine());
}
}


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

import java.io.*;
public class read
{
public static void main (String [] args) throws IOException
{
int num;
read in = new read();
num = in.readInt();
System.out.println (num);
}
public static int  readInt() throws java.io.IOException
{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader (input);
int Int;
Int = Integer.parseInt(reader.readLine());
return Int;
}
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: