When I did this program it compile fine, but when I run it I get this error:
java.util.NoSuc hElementExcepti on
at java.util.Scann er.throwFor(Sca nner.java:838)
at java.util.Scann er.next(Scanner .java:1461)
at java.util.Scann er.nextInt(Scan ner.java:2091)
at java.util.Scann er.nextInt(Scan ner.java:2050)
at Program4.main(P rogram4.java:30 )
I don't understand why because I put the input file in the directory of the program.
Here is the program:
import java.util.Scann er;
import java.io.*;
import java.text.Numbe rFormat;
public class Program4
{
public static void main(String [] args) throws IOException
{
File input = new File("spraying. txt");
Scanner scan = new Scanner(input);
int clientNumber,ty pe,acres;
double amountDue;
int number,total,co unter;
System.out.prin t("Client Number ");
clientNumber = scan.nextInt();
System.out.prin t("Type");
type = scan.nextInt();
total = acres = 0;
while(acres < 9999)
{
System.out.prin t("Acres");
acres = scan.nextInt();
total += type;
acres++;
}
if (acres > 0)
{
amountDue = calculateBill(t ype,acres);
NumberFormat fmt = NumberFormat.ge tCurrencyInstan ce();
fmt.format(amou ntDue);
//amountDue = (double) total/acres;
System.out.prin tln("The total billing is: "+acres+" numbers is "+amountDue );
}
else
System.out.prin tln("There was no input");
scan.close();
}
public static double calculateBill(i nt type,int acres)
{
int multiplier;
double grossBill;
if (type==1)
multiplier=1;
else if (type==2)
multiplier=3;
else if (type==3)
multiplier=4;
else if (type==4)
multiplier=6;
else
return -1;
grossBill = multiplier * acres;
if (acres > 1000)
grossBill -= 0.05*grossBill;
if (grossBill > 1500)
grossBill -= .1 * (grossBill-1500);
return grossBill;
}
}
java.util.NoSuc hElementExcepti on
at java.util.Scann er.throwFor(Sca nner.java:838)
at java.util.Scann er.next(Scanner .java:1461)
at java.util.Scann er.nextInt(Scan ner.java:2091)
at java.util.Scann er.nextInt(Scan ner.java:2050)
at Program4.main(P rogram4.java:30 )
I don't understand why because I put the input file in the directory of the program.
Here is the program:
import java.util.Scann er;
import java.io.*;
import java.text.Numbe rFormat;
public class Program4
{
public static void main(String [] args) throws IOException
{
File input = new File("spraying. txt");
Scanner scan = new Scanner(input);
int clientNumber,ty pe,acres;
double amountDue;
int number,total,co unter;
System.out.prin t("Client Number ");
clientNumber = scan.nextInt();
System.out.prin t("Type");
type = scan.nextInt();
total = acres = 0;
while(acres < 9999)
{
System.out.prin t("Acres");
acres = scan.nextInt();
total += type;
acres++;
}
if (acres > 0)
{
amountDue = calculateBill(t ype,acres);
NumberFormat fmt = NumberFormat.ge tCurrencyInstan ce();
fmt.format(amou ntDue);
//amountDue = (double) total/acres;
System.out.prin tln("The total billing is: "+acres+" numbers is "+amountDue );
}
else
System.out.prin tln("There was no input");
scan.close();
}
public static double calculateBill(i nt type,int acres)
{
int multiplier;
double grossBill;
if (type==1)
multiplier=1;
else if (type==2)
multiplier=3;
else if (type==3)
multiplier=4;
else if (type==4)
multiplier=6;
else
return -1;
grossBill = multiplier * acres;
if (acres > 1000)
grossBill -= 0.05*grossBill;
if (grossBill > 1500)
grossBill -= .1 * (grossBill-1500);
return grossBill;
}
}
Comment