Reputation: 41
i have a strange problem with my java compiler. I make a program with my friend( he send me the program with email and the problem is the same) and on his pc run in my pc don't run. I try to find all possible solution on stackoverflow, i do all that i read but it seems that there isn't solution, i also reinstall java with a differen version but don't run. The problem is the following:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at Geometriche.main(Geometriche.java:13)
The program is the following:
import java.util.*;
import java.io.*;
public class Geometriche{
public static void main (String [] arg)throws Exception{
Scanner sc = new Scanner(System.in);
double area = 0,a = 0;
int x = sc.nextInt();
Figura F[] = new Figura[x];
sc.nextLine();
for(int i=0; i<x; i++){
while(sc.hasNextLine()){
char s = sc.next().charAt(0);
switch(s){
case 'r':
double b = sc.nextDouble();
double al = sc.nextDouble();
F[i] = new Rettangolo(b,al);
a = F[i].getArea();
break;
case 'c':
double raggio = sc.nextDouble();
F[i] = new Cerchio(raggio);
a = F[i].getArea();
break;
case 't':
double lato1 = sc.nextDouble();
double lato2 = sc.nextDouble();
double lato3 = sc.nextDouble();
F[i] = new Triangolo(lato1,lato2,lato3);
a = F[i].getArea();
break;
case 'q':
double l = sc.nextDouble();
F[i] = new Quadrato(l);
a = F[i].getArea();
break;
}
area = area + a;
}
}
System.out.println("La somma delle aree e': "+area);
}
}
There are also 4 different classes that represent 4 different geomethric figures ( recthangle,circle,triangle,square) and an abstract class called figura. Please help me beacure on monday i have an exam very important Matteo
Upvotes: 2
Views: 87
Reputation: 358
Your code is throwing an exception due to a last newline character at the end of the file. I tried your code with a file I created with the input you provided and the output was as expected, no exception thrown. I added a newline character at the end of the file and, if we were to look at it, character by character, we would see something like this:
3\nt\n9\n12\n15\nc\n5\nq\n5\n
That last newline character is the cause of the exception, because you're reading it as well on while(sc.hasNextLine())
but there is no element after it, therefore a NoSuchElementException
, which was thrown when I changed the file.
Your friend managed to make this code work because he probably didn't include that last newline (he didn't press ENTER after typing the 5).
The solution is to delete the while, as there is no reason why you would have it anyway as you're already telling the forloop how many times to iterate, and inside the loop telling the Scanner how much to read on each condition.
The code below will only throw a NoSuchElementException
if you say you have a number of figures and don't describe them all on the file (i.e. putting 3 figures and only giving info to 2 of them).
public static void main (String [] arg) throws Exception {
String path = "C:\\folder\\myfile.txt";
Scanner sc = new Scanner(Paths.get(path), StandardCharsets.UTF_8.name());
double area = 0, a = 0;
int x = sc.nextInt();
Figura F[] = new Figura[x];
sc.nextLine();
for (int i = 0; i < x; i++) {
char s = sc.next().charAt(0);
switch (s) {
case 'r':
double b = sc.nextDouble();
double al = sc.nextDouble();
F[i] = new Rettangolo(b, al);
a = F[i].getArea();
break;
case 'c':
double raggio = sc.nextDouble();
F[i] = new Cerchio(raggio);
a = F[i].getArea();
break;
case 't':
double lato1 = sc.nextDouble();
double lato2 = sc.nextDouble();
double lato3 = sc.nextDouble();
F[i] = new Triangolo(lato1, lato2, lato3);
a = F[i].getArea();
break;
case 'q':
double l = sc.nextDouble();
F[i] = new Quadrato(l);
a = F[i].getArea();
break;
}
area = area + a;
}
System.out.println("La somma delle aree e': "+area);
}
Upvotes: 1