student
student

Reputation: 55

How to read from inputFileStream and split each line

I have to read from input file txtfile that look like mark;1001;3;4 there is a ';' between each variable. I know how to read it if it's in separate lines, but I can't read it if its in the same line.

This is how I start:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.Buffer;

public class Try {
    public static void main(String[] args) {
        String Name;
        int ID;
        Double quiz1 , quiz2;

        try {
            FileInputStream fileIN = new FileInputStream("input.txt");
            InputStreamReader inputST =new InputStreamReader(fileIN);
            BufferedReader  bufferRe = new BufferedReader(inputST);

            String line;

            while ((line = bufferRe.readLine()) != null) {
                // I tried many things, but nothing worked for me.
                // How could I use split here?
            }
        } catch (IOException e) {
            System.out.println("input is not found ");
        }
    }
}

Upvotes: 0

Views: 2720

Answers (5)

ema
ema

Reputation: 951

There is one more aproach using StreamTokenizer

try {
    FileInputStream fis = new FileInputStream("input.txt");
    Reader r = new BufferedReader(new InputStreamReader(fis));
    StreamTokenizer st = new StreamTokenizer(r);

    List<String> words = new ArrayList<>();
    List<Integer> numbers = new ArrayList<>();

     // print the stream tokens
     boolean eof = false;
     do {
        int token = st.nextToken();
        switch (token) {
           case StreamTokenizer.TT_EOF:
              System.out.println("End of File encountered.");
              eof = true;
              break;
           case StreamTokenizer.TT_EOL:
              System.out.println("End of Line encountered.");
              break;
           case StreamTokenizer.TT_WORD:
              words.add(st.sval);
              break;
           case StreamTokenizer.TT_NUMBER:
              numbers.add((int)st.nval);
              break;
           default:
              System.out.println((char) token + " encountered.");
              if (token == '!') {
                 eof = true;
              }
        }
     } while (!eof);

} catch (IOException e) {
    e.printStackTrace();
    System.out.println("input is not found ");
}

Upvotes: 0

adrCoder
adrCoder

Reputation: 3275

while ((line = bufferRe.readLine()) != null) {
    for (String retval : line.split(";", 2)) {
        System.out.println(retval);
    }
}

Output:

mark
1001;3;4

Upvotes: 0

Fedor Igumnov
Fedor Igumnov

Reputation: 201

Just use split method inside loop to get all your data in array.

String[] splited = line.split(";");

Upvotes: 0

Vitruvie
Vitruvie

Reputation: 2327

The simplest solution, which also works when you want things to work across newlines, is to use a Scanner with ; as its delimiter:

Scanner s = new Scanner(bufferRe);
s.useDelimiter(";");
while (s.hasNext()) {
    System.out.println(s.next());
}
-->
 mark
1001
3
4

This also allows you to use Scanner methods to eg. easily parse integers.

Upvotes: 1

Anthony Dito
Anthony Dito

Reputation: 3670

Using split is the way to go...

while ( ( line = bufferRe.readLine())!= null) {
    for (String splitVal : line.split(";") {
         //Do whatever you need to with the splitVal value.
         //In you example it iterate 4 times with the values mark 1001 3 4
    }
}

Upvotes: 2

Related Questions