import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Filereader {
public static void main(String[] args) throws IOException {
File fil = new File("trans1.txt");
FileReader inputFil = new FileReader(fil);
BufferedReader in = new BufferedReader(inputFil);
//Create scanner to read contents of a file
Scanner scanner = new Scanner(new File("trans1.txt"));
//Create an array to store contents of a file after reading them
String [] tall = new String [100];
int i = 0;
while(scanner.hasNext()){
//Create StringTokenizer object
StringTokenizer st = new StringTokenizer(scanner.next(),"< , { } >", true);
tall[i] = scanner.next();
System.out.println(tall[i]);
i++;
}
in.close();
}
}
I want to read contents of a file into an array using the code above and print the values stored in the array to screen. Each time i run the code i dont get the desired output,can someone please help. Contents of my file are as follows;
5
<0,{p1}>
<0,{p1}> -1-> <1>
<1,{p1}> -2-> <2><3>
<2,{p0}> -1-> <0>
<3,{p1}> -1-> <4>
<4,{p0}> -1-> <3>
Here’s how I’d do it:
And here’s the output I’d get if I run it against your file:
You don’t say what output you expected.