This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Filling an array from a file input stream  RSS feed

 
Martin Kyle
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to pass the file input for the excel.txt file to an array for a college project. The code I have so far is below. I can get it to read the file but get lost when trying to pass it to an array. It should also be able to read each line of the text file and ask the user for an input (int). Please can someone help me....

import java.io.*;
import java.util.*;

public class courseQuestions {

public static void main(String[] args) {


int grade;
int count = 0;
int runTot = 0;

try{

FileReader file = new FileReader("D:\\Java Files\\TNA Project\\TNA Project\\excel.txt");
BufferedReader buff = new BufferedReader(file);

boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
System.out.println(line);

}

buff.close();

} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}



Vector question = new Vector();

String [] course = {file};

for( int i = 0; i < course.length; i++ )
question.add(course[i]);



Iterator it = question.iterator();
while ( it.hasNext() )
{
System.out.println( it.next() );
grade = EasyIn.getInt();
count++;
runTot += grade;
}
}
}
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create an array for your Strings, you need to know its size.
The constructor String [] course = {file}; is very new for me - may it be wrong?

Count the lines while reading, and create your array with the result.
You have to read the file twice - the first time for counting, the second time to fill the array.

And please use code-tags.
Hit the code-button before pasting code, and after.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!