How to read in txt file

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 899 Views - Last Post: 02 August 2011 - 03:57 PM Rate Topic: -----

#1 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

How to read in txt file

Posted 02 August 2011 - 09:29 AM

Question is how would i be able to code within a program to read inside a txt file and set that info to two variable. For example if the txt file had the following info:

1
2

3
8

9
120

it would set 1 to var1 and 2 to var2 and say for example multiply them then output the product then move on to do 3 and 8.

How would i do that ?

This post has been edited by daonlyillwiz: 02 August 2011 - 09:29 AM

Is This A Good Question/Topic? 0
  • +

Replies To: How to read in txt file

#2 HiddenDragon  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 70
  • Joined: 25-November 10

Re: How to read in txt file

Posted 02 August 2011 - 09:32 AM

I would look into BufferedReader for that. It can read line by line which looks like what you want.

This post has been edited by HiddenDragon: 02 August 2011 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: How to read in txt file

Posted 02 August 2011 - 09:49 AM

Easier to use a Scanner. The first thing to do is to identify how many lines you have to skip
Was This Post Helpful? 0
  • +
  • -

#4 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

Re: How to read in txt file

Posted 02 August 2011 - 11:28 AM

import java.io.*;
public static void main(String[] args) throws IOException {

        int result= 0;
int number;
//create array of vars;
        int average=0;
        BufferedReader input = new BufferedReader(new FileReader("input.txt"));
        String lineRead;
        lineRead = input.readLine();
      
        
        
        while (lineRead != null) {
number= Integer.parseInt(lineRead);
          
            for (int i; i<//# of inputs;i++)
{
var[i]=number;
result = result + (var[i]+var[i+1]);

}

        }
        input.close();
        System.out.println(result);



Im not sure if this code is what you wanted, but post your results of this code and ill help you figure it out.

NOTE: you shouldnt leave lines in your input.txt file.

This post has been edited by Rhotor: 02 August 2011 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to read in txt file

Posted 02 August 2011 - 11:46 AM

Ok the program should read a sequence Of numbers and each number is separated by a space, each new sequence is separated by an empty line. I'm basically putting together a binary search tree that will read a text file with several sequences and for each sequence it will put the numbers in that sequence in order from least to greatest. That's pretty much what i want to do.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: How to read in txt file

Posted 02 August 2011 - 11:53 AM

Why use an array? Just use a list and use the .add() method.
Was This Post Helpful? 0
  • +
  • -

#7 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

Re: How to read in txt file

Posted 02 August 2011 - 11:56 AM

View Postipushmycar, on 02 August 2011 - 11:53 AM, said:

Why use an array? Just use a list and use the .add() method.

Because i have not learned the .add() method. I made that code from my knowladge.

Thanks though, will look into that method:)
Was This Post Helpful? 0
  • +
  • -

#8 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to read in txt file

Posted 02 August 2011 - 11:56 AM

I was thinking of using that method but I don't know how to incorporate it into reading a txt.

So it would .add() every number after each space then it will start a new process for the next sequence separated by an empty line.

This post has been edited by daonlyillwiz: 02 August 2011 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#9 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: How to read in txt file

Posted 02 August 2011 - 12:03 PM

Declare a list. (Arraylist or any list really)
Read in the first sequence of numbers up to the first new line. (I would use scanner like g00se pointed out)
Create your binary tree.
Repeat until there are no numbers.

This post has been edited by ipushmycar: 02 August 2011 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: How to read in txt file

Posted 02 August 2011 - 01:04 PM

Your file format is helpful for a human, but not a machine - it would be easier with no blank lines
Was This Post Helpful? 0
  • +
  • -

#11 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to read in txt file

Posted 02 August 2011 - 02:40 PM

I would like to do it w/o the blank lines however i have to since thats what the assignment calls for. How can i specifiy a blank line.
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: How to read in txt file

Posted 02 August 2011 - 02:50 PM

With the Scanner all you'd do is call Scanner.nextLine() and discard the result
Was This Post Helpful? 0
  • +
  • -

#13 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to read in txt file

Posted 02 August 2011 - 02:52 PM

Alright how can i get this to read and setnumbers into variables or add them to a list??

so say the first set of numbers is: 3 4 6 2 3 i want it to add all those numbers to a list that i can then insert into a tree.

import java.io.*;

public class add {
	public static void main(String args[]){
		try{
			FileInputStream fstream = new FileInputStream("textfile.txt");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			//Read File Line By Line
			while ((strLine = br.readLine()) != null)   {
				// Print the content on the console
				System.out.println (strLine);
			}
			//Close the input stream
			in.close();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,468
  • Joined: 27-December 08

Re: How to read in txt file

Posted 02 August 2011 - 03:05 PM

Why not just directly insert them into a Tree as you read them? To convert them from Strings to ints, you can use the Integer.parseInt() method.
Was This Post Helpful? 0
  • +
  • -

#15 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to read in txt file

Posted 02 August 2011 - 03:10 PM

How can i go about doing that? thats what has me at loss.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2