import java.io.*;
import java.util.Scanner;
//import java.util.ArrayList;
public class Reader {
// public static ArrayList tagwords;
public static void main(String[] aArgs) throws FileNotFoundException {
Reader parser = new Reader("D:\\Temp\\modela.txt");
parser.getLines();
log("Done.");
}
public Reader (String FileName){
fFile = new File(FileName);
}
public final void getLines() throws FileNotFoundException {
String line ;
Scanner scanner = new Scanner(new FileReader(fFile));
try {
//first use a Scanner to get each line
while ( scanner.hasNextLine() ){
line = scanner.next();
if (line.equals("MODEL_LIST")){
line = scanner.nextLine();
line=line.trim();
if (line.equals("{")){
line=scanner.nextLine();
line=line.trim();
if (line.equals("MODEL")) {
line=scanner.nextLine();
line=line.trim();
if (line.equals("{")){
do{
scanner.useDelimiter(" ");
line=scanner.nextLine();
String name = scanner.next();
String value = scanner.next();
log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
} while(!line.equals("}"));
}
}
}
}
}
}
finally {
scanner.close();
}
}
// PRIVATE
private final File fFile;
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
private String quote(String aText){
String QUOTE = "'";
return QUOTE + aText + QUOTE;
}
}
Hi i am trying to make a code in order for me to read this txt file
[textfile]
MODEL_LIST
{
MODEL
{
MODEL_ID “Ford Fiesta 1.0 170hp 4D not_auto”
CATEGORY epivatika
TYPE MIKRO
LITERS 1600
HORSEPOWER 106
NUMBER_OF_GEARS 6
TYPE_OF_TRANSMISSION MECHANICAL
LENGTH 1.26 M
WITDTH 1.56 M
HEIGTH 1.45 M
NUMBER_OF_DOORS 4
COST 35000
}
MODEL
{
.....
}
.....
}
[/textifle]
the output is
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Reader.getLines(Reader.java:31)
at Reader.main(Reader.java:12)
I want to be able to read the words passenger . SEDAN , 1600 ,.... in order for me to assign them to their object and continue my prgram but i keep getting this exception. Also because my code is kind of childish ( i am new to java ) i would like to hear some opinions about changes . P.S. It is very important that the text file is read as it is .
Thank you .
JavaNewbie!!, on 20 May 2012 - 03:19 PM, said:
import java.io.*;
import java.util.Scanner;
//import java.util.ArrayList;
public class Reader {
// public static ArrayList tagwords;
public static void main(String[] aArgs) throws FileNotFoundException {
Reader parser = new Reader("D:\\Temp\\modela.txt");
parser.getLines();
log("Done.");
}
public Reader (String FileName){
fFile = new File(FileName);
}
public final void getLines() throws FileNotFoundException {
String line ;
Scanner scanner = new Scanner(new FileReader(fFile));
try {
//first use a Scanner to get each line
while ( scanner.hasNextLine() ){
line = scanner.next();
if (line.equals("MODEL_LIST")){
line = scanner.nextLine();
line=line.trim();
if (line.equals("{")){
line=scanner.nextLine();
line=line.trim();
if (line.equals("MODEL")) {
line=scanner.nextLine();
line=line.trim();
if (line.equals("{")){
do{
scanner.useDelimiter(" ");
line=scanner.nextLine();
String name = scanner.next();
String value = scanner.next();
log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
} while(!line.equals("}"));
}
}
}
}
}
}
finally {
scanner.close();
}
}
// PRIVATE
private final File fFile;
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
private String quote(String aText){
String QUOTE = "'";
return QUOTE + aText + QUOTE;
}
}
Hi i am trying to make a code in order for me to read this txt file
[textfile]
MODEL_LIST
{
MODEL
{
MODEL_ID “Ford Fiesta 1.0 170hp 4D not_auto”
CATEGORY passenger
TYPE SEDAN
LITERS 1600
HORSEPOWER 106
NUMBER_OF_GEARS 6
TYPE_OF_TRANSMISSION MECHANICAL
LENGTH 1.26 M
WITDTH 1.56 M
HEIGTH 1.45 M
NUMBER_OF_DOORS 4
COST 35000
}
MODEL
{
.....
}
.....
}
[/textifle]
the output is
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Reader.getLines(Reader.java:31)
at Reader.main(Reader.java:12)
I want to be able to read the words passenger . SEDAN , 1600 ,.... in order for me to assign them to their object and continue my prgram but i keep getting this exception. Also because my code is kind of childish ( i am new to java ) i would like to hear some opinions about changes . P.S. It is very important that the text file is read as it is .
Thank you .

New Topic/Question
Reply



MultiQuote




|