• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Stupid java.IO question

Status
Not open for further replies.
I need to save a line of .TXT as an int

The file has 10 lines each with an integer and I need to be able to get each integer into a variable.

Thanks in advance
 

NetMapel

Guilty White Male Mods Gave Me This Tag
I haven't touch java in a while, but can you not save the line text in string first, then convert to int ?
 
Oh i forgot to mention i don't know anything about the .IO classes

How do you do that?

Please be as thorough as you can.
 

alejob

Member
My brother knows JAVA but he is in a remote beach thousands of miles away :p

I remember something about integer.parseint or something like that.
 

iapetus

Scary Euro Man
gamepro said:
I need to save a line of .TXT as an int

The file has 10 lines each with an integer and I need to be able to get each integer into a variable.

Thanks in advance

Warning: this code is horrible. If this is for a homework assignment and your teacher knows anything about Java, you will get beaten for not making anything based on this a lot more pretty.

Code:
import java.io.*;

public class FileReader {
  public static void main(String[] args) throws Exception {
    String filename = args[0];
    Reader r = new FileReader(filename);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    while (line != null) {
      System.out.println("Read: " + line);
      line = br.readLine();
    }
  }
}

Hell, if you use any of that line for line, I'll track you down and beat you myself. But it should give you a start.

Edit: And it isn't tested, because I have no compiler with me, so it's bound to be wrong in a couple of ways.

Edit2: That's just for reading in the lines of text - if you don't know how to convert to an integer, check out the Javadoc for the Integer class.
 
Thanks a lot iapetus, if you actually want those gmail invites PM me.

This is way ahead of anything i'm learning so it can be as ugly as pile of shit.

I know how to convert into an INT.
 

Ghost

Chili Con Carnage!
Way ahead of what you're learning? Well remember it then because pretty much everything you do in java requires you to read something in from some file then piss around with it and write it back out.


p.s. iapetus, couldnt just knock out the code for a 5-a-side footy simulation could ya? ;) That'd save me a lot of work.
 

Slo

Member
Guys, I really don't think it's a good idea to write these fundamental programs for people. If they don't learn the basics, things are going to spiral out of control for them very quickly later.
 

iapetus

Scary Euro Man
Slo said:
Guys, I really don't think it's a good idea to write these fundamental programs for people. If they don't learn the basics, things are going to spiral out of control for them very quickly later.

That's why it's ugly, unfinished code that doesn't do what he wants. Gives a starting point, but it still requires understanding to fix it up into something worthwhile.

Edit: I'd normally point at the Sun Java Tutorial, but it sucks for basic IO. The API is utterly useless here - you need to know which classes you want in order to find out how to use them.
 
Code:
loadInfo();


public void loadInfo()
    {
    	Reader r = new FileReader("Petsave.txt");
    	BufferedReader br = new BufferedReader(r);
    	String line = br.readLine();
	  	while (line != null) 
    	{
      	System.out.println("Read: " + line);
      	line = br.readLine();
      	//System.out.println(line);
I get a compile error
saying i gotta declare or throw the exception

Where do i put the IOexception thing
 

iapetus

Scary Euro Man
Same as always with exceptions - check the API to see which methods/constructors can throw IOException, and place the try/catch block accordingly. Getting it right is one of the trickier things in I/O, but it's something you can normally work out logically.

In general pretty much any I/O operation can fail with an exception, and the vast majority of exceptions are IOException or a subclass of IOException.
 
Making the whole code an try catch worked, i just had to make a few fields.

Thanks a lot everyone.

I wish the API explained all this. Dam piece of shit.
 
Status
Not open for further replies.
Top Bottom