Read until a newline (Java)

Stormlifter

2[H]4U
Joined
Oct 19, 2002
Messages
2,968
Assuming there are no identifiers for a new line in the input file how would I scan lets say, several int into a data structure, but store int on another line into another structure.

Here's psuedo code


read "ints" from file into array1 until new line
read "ints" from file into array2 until new line
read "ints" from file into arrayX until new line


What do you guys think? I tried all sorts of ideas, I think I could read the whole line as a string and parse it up, but I was wondering if anyone knew any tricks.
 
What do you mean by "no identifiers for a new line in the input"?
 
I don't see how readLine helps me. I need to have each of the bytes/integers read in separately, I don't want to have to parse out a string for numbers.

What I mean by "no identifiers" is, there isn't like a -1 or a particular character that denotes the end of a line.
 
I don't see how readLine helps me. I need to have each of the bytes/integers read in separately, I don't want to have to parse out a string for numbers.

What I mean by "no identifiers" is, there isn't like a -1 or a particular character that denotes the end of a line.

Were you hoping to use something akin to scanf()?

I suppose I don't quite fully understand your problem. From my view you were expressing trouble with reading a single line from an input stream.

You say there are no identifiers for a new line in the input file, but then go on to say that there is "[an] int on another line". I think you are confused here...either there are newline characters in the file or there are not.

I took a best guess with the information given...

Based on your pseudo code I would do something like this:
Code:
//build array 1
//read in a full line
String line = stdin.readLine();
//split on whitespace
String[] tokens = line.split("\\s+");
//tokens would now be {"123", "123", "123", ... }
int[] array1 = new int[tokens.length];
//convert Strings to ints
for(int i = 0; i < tokens.length; i++)
{
  try
  {
    array1[i] = Integer.parseInt(tokens[i]);
  }
  catch(NumberFormatException nfe)
  {
    System.out.printlnt("An invalid number was entered: " + tokens[i]);
  }
}

Is that what you were looking for?
 
Well. each int is on another line, but nothing explicitly in the file other than each digit is on another line, I'm assuming there is a newline character or notepad wouldn't put each int on another line IE:

3
1 2 3
2 5 3 6 7
2 6 7 5

I know how many lines there are, but have no clue where each line ends. What I need is...

array1 to have ( 1 2 3 )
array2 to have ( 2 5 3 6 7 )
array3 to have ( 2 6 7 5 )
 
I'm assuming there is a newline character or notepad wouldn't put each int on another line
You're right, there is. It's represented by the character '\n'. So if you took the entire contents of the file above as a single string, say s, then
s.equals("3\n1 2 3\n2 5 3 6 7\n2 6 7 5") would return true.

As others have mentioned, Java has a lot of built-in methods to do most of the work for you, but you'll learn a lot more if you try to do it manually. A couple of hints: Your solution should be in an array of arrays. In the above example, you'd declare a new int[3][] after reading the first line. Second, you'll need to know how long each array is before you start filling it out (since you need to create it first), so you should read the entire line before you start extracting numbers from it.
 
Well. each int is on another line, but nothing explicitly in the file other than each digit is on another line, I'm assuming there is a newline character or notepad wouldn't put each int on another line IE:

I know how many lines there are, but have no clue where each line ends. What I need is...

array1 to have ( 1 2 3 )
array2 to have ( 2 5 3 6 7 )
array3 to have ( 2 6 7 5 )

Go back and read through Genrelz post. His (or her...) code covers how to get all the ints from one line. Then loop through that code for each line that you have. So the first iteration will put ints 1,2,3 into array1. Then loop through the code again to put ints 2,5,3,6,7 into array2. Then another loop to put 2,6,7,5 into array3.
 

I'm seriously asking, actually. I'm confused by the requirements -- the first post says that he wants to read a whole line, but there is no token indicating the end of a line.
 
Post #6 in this thread makes me think it's a vector of vectors.
 
Post #6 in this thread makes me think it's a vector of vectors.

Excuse my Java, I'm very rusty, it's been 2 years...

Code:
Vector lines = new Vector();

try {
    BufferedReader br            = new BufferedReader(new FileReader("example.txt"));
    Vector         ints_per_line = new Vector();
    String         line          = new String();

    while ((line = br.readLine()) != null) {
        String[] s_ints = line.split(" ");
        ints_per_line.clear();
        for (String s_int : s_ints) {
            ints_per_line.add(Integer.parseInt(s_int));
        }
        lines.add(ints_per_line.clone());
    }
} catch (IOException e) {
    e.printStackTrace();
}

for (Iterator itr_cols = lines.iterator(); itr_cols.hasNext();) {
    for (Iterator itr_line = ((Vector)itr_cols.next()).iterator(); itr_line.hasNext();) {
        System.out.print(itr_line.next() + " ");
    }
    System.out.print("\n");
}

Output:
Code:
3 
1 2 3 
2 5 3 6 7 
2 6 7 5

Is this what you need Stormlifter?
 
according to #4 he doesnt want to read in entire strings and parse out the data.

seems like a pretty unrealistic specification though, but i suppose you could still do something similar. instead of parsing a line and adding the resulting ints to the vector afterward, you could read character by character, "building" an int as you move forward, and adding it to the ints_per_line vector when you hit whitespace. something like:

Code:
int currentInt = 0;
Vector vectorOfInts;
Vector vectorOfVectorOfInts;

foreach myChar in the character stream {
  if (myChar == <char in the '0'-'9' range> { //then it must be part of an int
    currentInt = currentInt*10 + Integer.valueOf(myChar); //add new digit as the last decimal place of your currentInt
  }

  else if (myChar == <whitespace, but not newline>) { //we've finished reading in an int
    vectorOfInts.add(currentInt); //add it to our vector
    currentInt = 0; reset current int
  }

  else if (myChar == <newline>) { //we've hit the end of the line
    vectorOfvectorOfInts.add(vectorOfInts); //add the current vector to the vector of vectors
    currentInt = 0; reset current int
  }
}

so something like that, for character by character reading with no string/char buffering
 
Back
Top