1 Replies - 639 Views - Last Post: 20 May 2012 - 03:33 PM Rate Topic: -----

#1 JavaNewbie!!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-May 12

Exception in thread "main" java.util.NoSuchElementException

Posted 20 May 2012 - 03:21 PM

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 .

View PostJavaNewbie!!, 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 .


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.util.NoSuchElementException

#2 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Exception in thread "main" java.util.NoSuchElementException

Posted 20 May 2012 - 03:33 PM

Please don't double post, just edit your original post if there was an error in it. :) Thank you. Also, I fixed your error, On line 26 you only have next(); you need to change that to nextLine();
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1