Dont know what I am doing wrong with my program help please!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djxposur
    New Member
    • Apr 2010
    • 3

    Dont know what I am doing wrong with my program help please!

    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;
    }
    }
  • djxposur
    New Member
    • Apr 2010
    • 3

    #2
    Nevermind the first question, I figured out what was wrong, now Im getting a compiler error saying "cannot find symbol - class clientNumber" I was not getting this before.

    Here is the new 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 infile = new Scanner(input);



    int clientNumber,ty pe,acres;
    double amountDue;
    int number,total,co unter;


    {

    clientNumber = infile.nextInt( );
    while(clientNum ber < 9999)
    type = infile.nextInt( );
    acres = infile.nextInt( );
    infile.close();
    total = acres = 0;
    System.out.prin tln("Client Number/////////"+"Type/////////"+ "Acres////////"+ "Amount Due");




    System.out.prin tln((clientNumb er)"///////"+ (type)"///////"+ (acres)"///////"+(amountDu e));



    total += type;
    acres++;
    }
    if (clientNumber== 0)
    {

    NumberFormat fmt = NumberFormat.ge tCurrencyInstan ce();
    amountDue = calculateBill(t ype,acres);
    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");

    }
    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

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      add a + after (clientNumber), otherwise it looks like a class cast.

      Comment

      • djxposur
        New Member
        • Apr 2010
        • 3

        #4
        Thank you , I fixed this now I am getting a different error for that line, could possible my while loop be in the wrong spot, I am trying to loop (clientNumber) (acres), and (type) untill it reads a 0

        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 infile = new Scanner(input);



        int clientNumber,ty pe,acres;
        double amountDue;
        int number,total,co unter;


        {

        clientNumber = infile.nextInt( );

        while(clientNum ber < 9999)

        type = infile.nextInt( );
        acres = infile.nextInt( );
        infile.close();
        total = acres = 0;
        System.out.prin tln("Client Number/////////"+"Type/////////"+ "Acres////////"+ "Amount Due");




        System.out.prin tln((clientNumb er)+"///////"(type)+"///////"(acres)+"///////"+(amountDu e));



        total += type;
        acres++;
        }
        if (clientNumber== 0)
        {

        NumberFormat fmt = NumberFormat.ge tCurrencyInstan ce();
        amountDue = calculateBill(t ype,acres);
        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");

        }
        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

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Not sure how your algorithm is working but probably:
          if (clientNumber == 0) break;

          Adding code tags will get you a slightly faster response to your questions.
          [ code ] code here [ / code ] (remove spaces)

          Comment

          Working...