Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Runtime error, illegal argument exception

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Runtime error, illegal argument exception

    So my assignment is

    1)To create file and output ten numbers to said file.
    2)To read as input the numbers in the file and output the # of numbers, average, and total sum
    3)Output results to the same file

    My problem is a runtime error, the compiler doesn't catch it.

    I'm not really sure what to do. We're told to throw all exceptions for now because we havent learned how to deal with them. I have throws IOException in the main method header, but I have a problem with the program. When I try to run it the error code is

    Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "##.#0"
    at java.text.DecimalFormat.applyPattern(DecimalFormat .java:2610)
    at java.text.DecimalFormat.<init>(DecimalFormat.java: 435)
    at QudratullahMommandi_3_4.main(QudratullahMommandi_3 _4.java:25)

    I don't see how the pattern is malformed, but debugging language is straight up esoteric to me at this point, am I using the text formatting util wrong or something? Also any help is appreciated, this is my second post in two days so hopefully this isnt considered spamming the forum, and a ty ahead of time to anyone who takes the time to read this.

     
    import java.io.*;
    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class QudratullahMommandi_3_4
    {
     
         public static void main (String[]args) throws IOException
        {  
     
         int numberOfNumbers = 0;
         double totalSum = 0;
         double average;
     
         //Formatting object to format average and # of nums
         DecimalFormat formatter = new DecimalFormat("##.#0");
     
         // Creating s file and outputting numbers to it
         PrintWriter newOutput = new PrintWriter("QudratMommandi_S_04_Output.txt");
     
         newOutput.println("10.5");
         newOutput.println("10 55");
         newOutput.println("7.1");
         newOutput.println("55.5");
         newOutput.println("21.5 22.1");
         newOutput.println("12.5 78.9");
         newOutput.println("77.7");
     
         // Closing the file so the numbers are saved
         newOutput.close();
     
         //Creating a file object refering to the file
         //Creating a scanner object refering the file object for input
         File inputSource = new File("QudratMommandi_s_04_Output.txt");
         Scanner inputFile = new Scanner(inputSource);
     
     
         while (inputFile.hasNext())
          {
          //Tally of the number of input numbers, used to calculate average later on
          numberOfNumbers++;
          //reading input and saving it to variable
          double num = inputFile.nextDouble();
          //Total sum holds all the numbers read added subsequently to one another
          totalSum += num;
          }
          //closing file to save data
          inputFile.close();
     
         //Calculating average
         average = totalSum/numberOfNumbers;
     
         //opening file for output
         FileWriter fwriter = new FileWriter("QudratMommandi_S_04_Output.txt", true);
         PrintWriter finalStage = new PrintWriter(fwriter);
         //Outputting new data into file
         finalStage.println(numberOfNumbers);
         finalStage.println(formatter.format(totalSum));
         finalStage.println(formatter.format(average));
     
         System.out.println(numberOfNumbers);
         System.out.println(totalSum);
         System.out.println(average);
     
     
         System.exit(0);
     
          }     
    }


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Runtime error, illegal argument exception

    Try changing the ##.#0 to ##.00
    - Jim

  3. #3
    Junior Member
    Join Date
    May 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Runtime error, illegal argument exception

    Those kind of errors occur a lot and it;s important to learn how to deal with them. There are many guides on the web you can use. I you want you can try using some programs, like checkmarx and others to help you with it.
    Good luck.

Similar Threads

  1. Illegal Argument Exception: input == null
    By Ecen in forum Exceptions
    Replies: 4
    Last Post: January 8th, 2014, 12:29 PM
  2. Re: Illegal Argument Exception: input == null
    By eglentine in forum Object Oriented Programming
    Replies: 1
    Last Post: January 8th, 2014, 09:37 AM
  3. [SOLVED] Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project
    By Andrew R in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2013, 12:02 PM
  4. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM