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 8 of 8

Thread: Need major help with a program for class

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need major help with a program for class

    I am having trouble with my class assignment. I have gotten myself confused and don't know how to proceed. also i'm really having a problem thinking this through, and don't know where to continue working or what to throw away from my code. Any input or help would be immensely helpful as i am at a dead end currently. The bottom methods are all commented out as I want to be able to finish the first part first. maybe if i can get that done i can do the rest on my own. also, if you can tell me how to tidy up my code and make it more readable, that would be helpful, as its starting to confuse even me at this point.

    this is my class assignment:

    You have been asked to build a program to read high and low temperatures for each month from a data file. Based upon the data, you will report temperature statistics to both the output window and a file as outlined below. Output should include a heading and be formatted in columns. The table gridlines are not required. Your program design and solution should implement methods as described in Chapter 5.
    Input – All program input will be through a data file
    Input File: your program should prompt for the name of the input file
    You should test your program using at least two sample executions. One with
    MonthlyTemperatures.txt (provided in Blackboard)
    You will create a second text file to test your program
    Files should include month name, highest temperature and lowest temperature for each month.
    this is what the .txt looks like
    January 72 -12
    February 75 -5
    March 80 8
    April 98 15
    May 98 29
    June 100 28
    July 103 48
    August 106 40
    September 102 35
    October 90 26
    November 85 14
    December 76 -10

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
    /**
       This program applies line numbers to a file.
    */
    public class TempCheck
    {
       public static void main(String[] args) throws FileNotFoundException
       {
            //Variables
            int lineNumber = 1;
            String line; 
            String[] data = new String[12];
            int[] temps = new int[12];
     
            // Prompt for the input and output file names
     
          Scanner console = new Scanner(System.in);
          System.out.print("Input file: ");
          String inputFileName = console.next();
          System.out.print("Output file: ");
          String outputFileName = console.next();
     
          // Construct the Scanner and PrintWriter objects for reading and writing
     
          File inputFile = new File(inputFileName);
          Scanner in = new Scanner(inputFile);
          PrintWriter out = new PrintWriter(outputFileName);
     
            for (int i = 0;  i < 12; i++)
            {
     
          line = in.nextLine();
     
            data[i] = line;
     
            // Data being sent to the other methods
            temps = firstTemp(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11]);
     
            }
            /**
            all of the months printed with there 
            corresponding tempuratures, 
            the averages of the tempuratures,
            and the rangeof the tempuratures
            */
            System.out.println ("Month         Tempuratures    Average    Range");
            System.out.println ("             ______________  _________  _______");
            System.out.println ("January      |            |  |       |  |     |");
            System.out.println ("Febryary     |            |  |       |  |     |");
            System.out.println ("March        |            |  |       |  |     |");
            System.out.println ("April        |            |  |       |  |     |");
            System.out.println ("May          |            |  |       |  |     |");
            System.out.println ("June         |            |  |       |  |     |");
            System.out.println ("July         |            |  |       |  |     |");
            System.out.println ("August       |            |  |       |  |     |");
            System.out.println ("September    |            |  |       |  |     |");
            System.out.println ("October      |            |  |       |  |     |");
            System.out.println ("November     |            |  |       |  |     |");
            System.out.println ("December     |            |  |       |  |     |");
            System.out.println ("             --------------  ---------  -------");
     
            in.close();
            out.close();
       }
        /** 
          Extracts the first tempurature
          @param line a line containing a month name, followed by a tempurature value
          @return the first Temp associated with the month
       */
       public static int[] firstTemp(String jan,String feb,String mar,String apr,String may,String jun,String jul,String aug,String sep,String oct,String nov,String dec,)
       {
            //array for the first temps
            int[] temps = new int[12];
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(jan.charAt(i))) 
            { 
                i++; 
            }
          // Extract and convert the value
          temps[0] = jan.parseInteger(jan.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(feb.charAt(i))) 
            { 
                i++; 
            }
          // Extract and convert the value
          temps[1] = feb.parseInteger(feb.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(mar.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[2] = mar.parseInteger(mar.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(apr.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[3] = apr.parseInteger(apr.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(may.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[4] = may.parseInteger(may.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(jun.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[5] = jun.parseInteger(jun.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(jul.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[6] = jul.parseInteger(jul.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(aug.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[7] = aug.parseInteger(aug.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(sep.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
            temps[8] = sep.parseInteger(sep.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(oct.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[9] = oct.parseInteger(oct.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(nov.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[10] = nov.parseInteger(nov.substring(i).trim());
     
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(dec.charAt(i)))
            { 
                i++; 
            }
          // Extract and convert the value
          temps[11] = dec.parseInteger(dec.substring(i).trim());
     
            return temps;
        }
        /**
            /** 
          Extracts the second tempurature
          @param line a line containing a month name, followed by a tempurature value
          @return the second Temp associated with the month
        *
        public static int[] secondTemp(
        {
            //do stuff
        }
     
            /**Averages all of the tempuratures from each month
            @
            @return the averages for each month
            *
        public static int averageTemp (
        {
            //do stuff
        }
            /** Records the range of all of the tempuratures for each month
            @
            @return the range for each month
            *
        public static int tempRange (
        {
            //do stuff
        }
        */
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need major help with a program for class

    First thing you need to do is clean up the compiler errors. I got over 100 errors when I tried to compile your code.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need major help with a program for class

    I changed the code, now im getting a run time error code.
    Exception in thread "main" java.lang.NumberFormatException: For input string: "72 -12"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at TempCheck1.firstTemp(TempCheck1.java:86)
    at TempCheck1.main(TempCheck1.java:43)

    Also, I believe this is because its trying to turn a string wit a non number character into an integer, but am not sure. also i still don't know hhow to separate both of the temperatures from a single line, any thoughts?

    ----jGRASP wedge2: exit code for process is 1.
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
    /**
       This program applies line numbers to a file.
    */
    public class TempCheck1
    {
       public static void main(String[] args) throws FileNotFoundException
       {
    		//Variables
    		int lineNumber = 1;
    		String line; 
    		String[] data = new String[12];
    		int[] temps = new int[12];
     
    		// Prompt for the input and output file names
     
          Scanner console = new Scanner(System.in);
          System.out.print("Input file: ");
          String inputFileName = console.next();
          System.out.print("Output file: ");
          String outputFileName = console.next();
     
          // Construct the Scanner and PrintWriter objects for reading and writing
     
          File inputFile = new File(inputFileName);
          Scanner in = new Scanner(inputFile);
          PrintWriter out = new PrintWriter(outputFileName);
     
    		for (int i = 0;  i < 12; i++)
    		{
     
          line = in.nextLine();
     
    		data[i] = line;
     
    		}
     
    		// Data being sent to the other methods
    		temps = firstTemp(data);
     
    		/**
    		all of the months printed with there 
    		corresponding tempuratures, 
    		the averages of the tempuratures,
    		and the rangeof the tempuratures
    		*/
    		System.out.println ("Month         Tempuratures    Average    Range");
    		System.out.println ("             ______________  _________  _______");
    		System.out.println ("January      |            |  |       |  |     |");
    		System.out.println ("Febryary     |            |  |       |  |     |");
    		System.out.println ("March        |            |  |       |  |     |");
    		System.out.println ("April        |            |  |       |  |     |");
    		System.out.println ("May          |            |  |       |  |     |");
    		System.out.println ("June         |            |  |       |  |     |");
    		System.out.println ("July         |            |  |       |  |     |");
    		System.out.println ("August       |            |  |       |  |     |");
    		System.out.println ("September    |            |  |       |  |     |");
    		System.out.println ("October      |            |  |       |  |     |");
    		System.out.println ("November     |            |  |       |  |     |");
    		System.out.println ("December     |            |  |       |  |     |");
    		System.out.println ("             --------------  ---------  -------");
     
    		in.close();
    		out.close();
       }
     
    	/** 
          Extracts the first tempurature
          @param line a line containing a month name, followed by a tempurature value
          @return the first Temp associated with the month
       */
       public static int[] firstTemp (String data[])
       {
    		//array for the first temps
    		int[] temps = new int[12];
          int i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[0].charAt(i))) 
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[0] = Integer.parseInt(data[0].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[1].charAt(i))) 
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[1] = Integer.parseInt(data[1].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[2].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[2] = Integer.parseInt(data[2].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[3].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[3] = Integer.parseInt(data[3].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[4].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[4] = Integer.parseInt(data[4].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[5].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[5] = Integer.parseInt(data[5].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[6].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[6] = Integer.parseInt(data[6].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[7].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[7] = Integer.parseInt(data[7].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[8].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
         	temps[8] = Integer.parseInt(data[8].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[9].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[9] = Integer.parseInt(data[9].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[10].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[10] = Integer.parseInt(data[10].substring(i).trim());
     
          i = 0; // Locate the start of the first digit
          while (!Character.isDigit(data[11].charAt(i)))
    		{ 
    			i++; 
    		}
          // Extract and convert the value
          temps[11] = Integer.parseInt(data[11].substring(i).trim());
     
    		return temps;
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need major help with a program for class

    NumberFormatException: For input string: "72 -12"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at TempCheck1.firstTemp(TempCheck1.java:86)
    At line 86 in your code you are calling parseInt() with the String "72 -12".
    The parseInt method does not think that is a valid numeric value and throws an exception.
    You need to pass only valid numeric digits to parseInt, sucu as "72" or "12"

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need major help with a program for class

    Improving the world one idiot at a time!

  6. The Following 2 Users Say Thank You to Junky For This Useful Post:

    Norm (November 20th, 2011), Tjstretch (November 21st, 2011)

  7. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need major help with a program for class

    Yes, Junky, im trying to get help from two different sites.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need major help with a program for class

    So on which one do you want people to spent time on your problem?
    We're not in competition.

  9. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need major help with a program for class

    Problem solved, thanks for the help.

Similar Threads

  1. Two class program help
    By BuhRock in forum Object Oriented Programming
    Replies: 4
    Last Post: July 4th, 2012, 05:27 PM
  2. Major Help Needed!
    By Rads23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 13th, 2011, 10:03 AM
  3. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  4. need help in major project
    By h313 in forum The Cafe
    Replies: 1
    Last Post: August 1st, 2010, 10:58 AM
  5. new program (lotto.class)
    By james137 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2009, 05:22 PM