Reading a String Input Till Newline in Java

  • Forums
  • Bits & Bytes
  • Webmastering & Programming
You are using an out of date browser. It may non display this or other websites correctly.
You should upgrade or use an culling browser.

Read until a newline (Java)

  • Thread starter Stormlifter
  • Start date
  • #one
Joined
Oct 19, 2002
Letters
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.

Hither'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 lot guys think? I tried all sorts of ideas, I call up I could read the whole line as a string and parse it upwardly, just I was wondering if anyone knew any tricks.

  • #2

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
What practise you mean by "no identifiers for a new line in the input"?
  • #3
Joined
May 12, 2005
Letters
395
I hear BufferedReader has this peachy readLine method...
  • #4
Joined
Oct nineteen, 2002
Messages
2,968
I don't see how readLine helps me. I demand to accept each of the bytes/integers read in separately, I don't desire 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 graphic symbol that denotes the finish of a line.

  • #five
Joined
May 12, 2005
Messages
395
I don't see how readLine helps me. I need to accept each of the bytes/integers read in separately, I don't desire to have to parse out a string for numbers.

What I mean by "no identifiers" is, at that place 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.

Yous say there are no identifiers for a new line in the input file, only then proceed to say that there is "[an] int on some other line". I retrieve you lot are confused hither...either at that place are newline characters in the file or there are not.

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

Based on your pseudo code I would exercise something like this:

                              //build assortment i //read in a full line Cord line = stdin.readLine(); //separate on whitespace String[] tokens = line.split("\\s+"); //tokens would now exist {"123", "123", "123", ... } int[] array1 = new int[tokens.length]; //convert Strings to ints for(int i = 0; i < tokens.length; i++) {   attempt   {     array1[i] = Integer.parseInt(tokens[i]);   }   grab(NumberFormatException nfe)   {     Organization.out.printlnt("An invalid number was entered: " + tokens[i]);   } }                            

Is that what you were looking for?
  • #vi
Joined
Oct 19, 2002
Messages
2,968
Well. each int is on some other line, but zippo 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:
three
1 2 3
two 5 3 6 seven
2 6 7 5

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

array1 to accept ( 1 2 3 )
array2 to accept ( 2 five 3 half-dozen 7 )
array3 to accept ( ii 6 7 5 )

  • #7

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
What I mean by "no identifiers" is, at that place isn't like a -1 or a particular character that denotes the end of a line.

At that place isn't? Non even a newline graphic symbol?
  • #8
Joined
Oct 26, 2005
Messages
2,340
I'm assuming there is a newline character or notepad wouldn't put each int on some other line
You lot're right, there is. It'due south represented by the character '\n'. So if you took the unabridged contents of the file above as a single cord, say s, then
due south.equals("3\n1 two 3\n2 5 3 vi 7\n2 half-dozen 7 5") would return true.

As others have mentioned, Java has a lot of born methods to practice most of the work for you, simply 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[three][] afterward reading the outset line. 2nd, you'll need to know how long each assortment is before you beginning filling it out (since you need to create it start), and then you lot should read the unabridged line earlier you showtime extracting numbers from information technology.

  • #nine
Joined
May 31, 2002
Messages
384
Well. each int is on another line, just nothing explicitly in the file other than each digit is on another line, I'm bold at that place is a newline character or notepad wouldn't put each int on another line IE:

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

array1 to accept ( 1 2 3 )
array2 to have ( two 5 3 6 7 )
array3 to have ( 2 half dozen vii five )


Get back and read through Genrelz mail service. His (or her...) code covers how to get all the ints from i line. Then loop through that code for each line that yous take. So the starting time iteration will put ints 1,2,iii into array1. Then loop through the lawmaking once again to put ints 2,5,3,half-dozen,vii into array2. Then some other loop to put 2,6,7,5 into array3.
  • #10
Joined
November six, 2005
Letters
614
In that location isn't? Not even a newline character?

:D haha
  • #eleven

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
:D haha

I'm seriously asking, actually. I'm dislocated by the requirements -- the first mail service says that he wants to read a whole line, only there is no token indicating the end of a line.
  • #12
Joined
Mar twenty, 2007
Messages
52
can u exercise a nested loop and put the ints in their place? look is the array linear or 2nd?
  • #13

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
Post #half dozen in this thread makes me retrieve it'south a vector of vectors.
  • #xiv
Joined
Dec 18, 2006
Messages
105
Mail service #vi in this thread makes me think information technology'southward a vector of vectors.

Excuse my Java, I'm very rusty, it'south been 2 years...
                              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()) != nothing) {         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 together(ints_per_line.clone());     } } catch (IOException due east) {     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.impress(itr_line.next() + " ");     }     System.out.print("\n"); }                            

Output:
                              3  one 2 three  two v 3 6 seven  2 6 7 5                            

Is this what you demand Stormlifter?
  • #15
Joined
May 31, 2005
Letters
864
co-ordinate to #four he doesnt want to read in entire strings and parse out the information.

seems like a pretty unrealistic specification though, but i suppose you lot could nonetheless practise something like. instead of parsing a line and calculation the resulting ints to the vector afterward, you could read graphic symbol by character, "building" an int as y'all motion forrad, and adding information technology to the ints_per_line vector when you hit whitespace. something similar:

                              int currentInt = 0; Vector vectorOfInts; Vector vectorOfVectorOfInts;  foreach myChar in the character stream {   if (myChar == <char in the '0'-'ix' range> { //and so it must be role of an int     currentInt = currentInt*ten + Integer.valueOf(myChar); //add new digit as the concluding decimal place of your currentInt   }    else if (myChar == <whitespace, merely not newline>) { //we've finished reading in an int     vectorOfInts.add together(currentInt); //add together it to our vector     currentInt = 0; reset electric current int   }    else if (myChar == <newline>) { //we've hit the end of the line     vectorOfvectorOfInts.add together(vectorOfInts); //add the current vector to the vector of vectors     currentInt = 0; reset electric current int   } }                            

so something similar that, for character by character reading with no string/char buffering
  • Forums
  • Bits & Bytes
  • Webmastering & Programming

schreinersteened1981.blogspot.com

Source: https://hardforum.com/threads/read-until-a-newline-java.1172979/

0 Response to "Reading a String Input Till Newline in Java"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel