Click to See Complete Forum and Search --> : Troubleshooting Weird Error Message


nvibest
10-30-2008, 07:31 PM
Hello all,

This is my first post at the saloon. I've been writing a program that puts data from a .dat file into a table that I formatted. It's been going smoothly so far but recently I have been getting this error message and I don't know what it could be from. [banghead]



Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1317)
at Salary.main(Salary.java:14)


Here are the two classes that are involved with it.



import java.lang.Object;
public class TableFormat {
//May need to change encapsulation
private String Hello;
private int width;
private char alignment;
public static boolean left;
public static boolean right;

//Constructor
public TableFormat(){

}

/*****This method positions the String s in a desired field width
//then aligns padding on either the right or left
*
* :----- :
* : :
* :----- :xxxx PADDING
* :
* :
* :
*/
public static String padString ( String s, int width, char alignment ){
if (alignment == 'L' || alignment == 'l'){
System.out.println("pad to the left " + width + " units.");
int space = width;
int spNeeded = s.length();
int truncate = (width - spNeeded);
System.out.println("width: "+width+" spaces: "+space);
for (int i = 0; i < width; i++ ){
System.out.print(s );
}
System.out.println(s);
truncate +=1;
System.out.println("truncate: " + truncate);

int sLeng = s.length();
System.out.println("string length: "+ sLeng);
s = s.substring(0, (sLeng-1));
System.out.println(s);


}


else if (alignment == 'R' || alignment == 'r'){
boolean right = true;
boolean left = false;
System.out.println("pad to the right " + width + " units.");
for (int i = 0; i < width; i++ ){
System.out.print(" ");
}

}

return s;

}
//This method rounds integer n off to the
//nearest 1,, 10s, 100s... place
public static int round (int n, int power){
switch(power){
case 0:
n = n;
break;
case 1:
for(int i=0; i< 9; i++){
if(n % 10 ==0){

}
else{
n++;
}
}
break;
case 2:
for(int i=0; i< 99; i++){
if(n % 100 ==0){

}
else{
n++;
}
}
break;
case 3:
for(int i=0; i< 999; i++){
if(n % 1000 ==0){
break;
}
else{
n++;
}
}
break;
}
return n;

}


//This method inserts commas in the 1000s, 1000000s,.
//..place of integer n and return the result as a string

public static String addCommas(String n){
if(n.length() < 4){
return n;
}
return addCommas(n.substring(0, n.length() - 3))
+ "," + n.substring(n.length() - 3, n.length());
}



}





import java.util.Scanner;
import java.io.*;
public class Salary{
public static void main(String[] args) throws IOException{
TableFormat tab = new TableFormat();
String first, last, salary;
Scanner fileScan, nameScan;
fileScan = new Scanner ( new File ("employee.dat"));
/************
* Read File*
************/
int i = 0;
while( i <= 10){
first = fileScan.next();
last = fileScan.next();
salary = fileScan.next();
System.out.print(" "+first);
System.out.print(" "+last);
System.out.print(" "+salary);
System.out.println();
i++;
}

System.out.println("hey");





}

}




and here is the text in the data file (employee.dat)...



Mark Billinghouse 1345231
Jennifer Sutter 52114
Jessica Frazier 100214
Kathryn Carrigan 15235
Samuel Johnson 881945
Benjamin Jensen 1785
Elizabeth Adams 45678
Elise Killy 2913467
Austin Sasiak 156000
Stephen Nickerson 990



Any help would be greatly appreciated.

Thanks,

n

chazzy
10-30-2008, 09:12 PM
when you do this..


while( i <= 10){
first = fileScan.next();
last = fileScan.next();
salary = fileScan.next();


You read 3 lines, not 1. You should read 1 line, and split it at " "