java, returning strings of ints from external file

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1398 Views - Last Post: 11 July 2010 - 04:04 PM Rate Topic: -----

#1 Guest_interningsucks*


Reputation:

java, returning strings of ints from external file

Posted 09 July 2010 - 07:16 AM

ok so with this code, it returns 27,000 which is the first integer in the file. But it won't keep going, even if i put a for loop for the S.O.P's. so how can i make this code be able to return a whole set of integers from the file?

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class InputFileDeclared 
{
	private FileInputStream in;

	public InputFileDeclared(String tTrans1) throws FileNotFoundException 
	{
		in = new FileInputStream("C:/Users/Chris/Documents/proofWork/tTrans1.txt");
	}

	public String getWord() throws IOException 
	{
		int c=0;
		StringBuffer buf = new StringBuffer();

		do
		{
			c = in.read();
			if (Character.isSpace((char) c))
				return buf.toString();
			else
				buf.append((char) c);
		}

		while (c != -1);
		{
			return buf.toString();
		}
	}
	public static void main(String[] args) throws java.io.IOException 
	{	
		InputFileDeclared file = new InputFileDeclared("C:/Users/Chris/Documents/proofWork/tTrans1.txt");
		System.out.print(file.getWord());
		//			System.out.print(file.getWord());
		//			System.out.print(file.getWord());
		//			System.out.print(file.getWord());
		//			System.out.print(file.getWord());
		//			System.out.print(file.getWord());
		//			System.out.println(file.getWord());
	}
}



Edited by macosxnerd101: Welcome to DIC! :) Please post your code using code tags, like so: :code:.

This post has been edited by macosxnerd101: 09 July 2010 - 08:21 AM


Is This A Good Question/Topic? 1

Replies To: java, returning strings of ints from external file

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 911
  • View blog
  • Posts: 3,180
  • Joined: 12-May 09

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 07:53 AM

Once return is called, the method is exiting. So even with the loop, it returns the first value, and control is back in your main method. Try putting the loop around the call to getWord:

while (!end_of_file)
  System.out.println(file.getWord());



Also, please post your code between code tags to make it more readable in the future.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,750
  • Joined: 20-September 08

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 07:55 AM

Can you post an example file please?
Was This Post Helpful? 0
  • +
  • -

#4 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 07:56 AM

ok, i was wondering why it wasn't staying spaced out haha. i'll try that, thanks man.

@goose, the file i'm working with is 27,000 lines long and probably 50 spaces per line
Was This Post Helpful? 0
  • +
  • -

#5 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:00 AM

If you want to read every single line from a txt-File, there's a easier method than working with FileInputStreams.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class ReadFile {
	String line_text;
	int line;
	
	public static void main(String[] args) throws IOException{
		try {
			BufferedReader in = new BufferedReader(new FileReader("file.txt"));
			// Reading the Document:
			String line_text = "";
			int line = 1;
			while ((line_text = in.readLine()) != null){
				System.out.println("Line "+line+ ": "+line_text);
				line++;
			}
			// Closing Reader
			in.close();			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}



This reads every line from the Document "file.txt" and prints it out.

If you need Int variables, you can add this to the while-loop:
int your_int = Integer.parseInt(line_text);

This will give you an Intenger, if the content in your file really is an Intenger...

Greetings: Luke

This post has been edited by MrLuke187: 09 July 2010 - 08:02 AM

Was This Post Helpful? 2
  • +
  • -

#6 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:01 AM

xclite, it worked. thanks for the help.

thanks luke, i will try that as well to see which one is more efficient.

This post has been edited by irmoathlete: 09 July 2010 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2731
  • View blog
  • Posts: 15,808
  • Joined: 24-September 08

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:04 AM

First off, :code:. Second, so you're trying to read a file full of ints and display them in a list? Why not just put this as your main:

public static void main(String[] args) {

try{
InputFileDeclared file = new InputFileDeclared("C:/Users/Chris/Documents/proofWork/tTrans1.txt");

while(file.hasNext()) {
System.out.println(file.getWord());
} // end while

} // end try

catch(exception e) {
System.out.println(e.getMessage);
} // end catch

} // end main method



This should put each int on a new line for ya until the file ends.



EDIT: Holy crap everybody posted at once, that or I'm just slow lol.

This post has been edited by NeoTifa: 09 July 2010 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,750
  • Joined: 20-September 08

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:08 AM

A Scanner would probably be easiest:

Scanner s = new Scanner(inputFile);
while(s.hasNextInt()) {
	int i = s.nextInt();
	// Process it
}
s.close();	

Was This Post Helpful? 0
  • +
  • -

#9 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:11 AM

ok heres what this is for. I have to go into the external file, get a certain list of ints, put them through my algorithm to return T-representation of 2 lists. once that happens i have to print to an external .txt file. then im done.
Was This Post Helpful? 0
  • +
  • -

#10 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:18 AM

If you're sure that your input File contains only Int's, you can use my or g00se method, they botch return Intengers.

To write the list's your creating in an other file, you can use a BufferedWriter :P

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#11 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 08:21 AM

the list doesn't only contain ints, i will post some example lines from the text:

27000 = + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 15^2 + 22^2 + 28^2 + 33^2 + 42^2 + 45^2 + 63^2 + 72^2 + 105^2
26999 = + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 14^2 + 20^2 + 28^2 + 40^2 + 44^2 + 48^2 + 72^2 + 80^2 + 88^2
26998 = + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 14^2 + 22^2 + 33^2 + 35^2 + 42^2 + 45^2 + 54^2 + 72^2 + 108^2


Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2731
  • View blog
  • Posts: 15,808
  • Joined: 24-September 08

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 09:07 AM

View Postirmoathlete, on 09 July 2010 - 09:11 AM, said:

ok heres what this is for. I have to go into the external file, get a certain list of ints, put them through my algorithm to return T-representation of 2 lists. once that happens i have to print to an external .txt file. then im done.



Could you further expand on this? I tried googling t-representation, but I didn't get anything. :(
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,750
  • Joined: 20-September 08

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 09:27 AM

Quote

the list doesn't only contain ints


OK - in that case use next() instead of nextInt
Was This Post Helpful? 0
  • +
  • -

#14 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 10:13 AM

ok i'm working on computer assisted proofs for the math department. t-representation can be called 1-representable, 2-representable and so on. theres a lot of math behind it. the math part isnt the problem its just the external sources returning the int lists that i was having trouble with. So if i just use .next() it should work then.
Was This Post Helpful? 0
  • +
  • -

#15 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: java, returning strings of ints from external file

Posted 09 July 2010 - 11:38 AM

View Postirmoathlete, on 09 July 2010 - 05:13 PM, said:

So if i just use .next() it should work then.


It isn't that easy :P. If you use the next() method, you're getting a String from the whole line from your document. If this is all you need, you're cool with that.

But if you need your Programm to perform those Math-Operations you have in your file, you'll have to split the String up...

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2