Results 1 to 14 of 14
  1. #1
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default I need help with my school assignment, please

    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



    Java Code:
    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
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    What do you want to do with the method parseInteger if you write it like this:
    jan.parseInteger(jan.substring(i).trim()) ?

    To make a Integer from a String you can use:
    Java Code:
    Int i=Integer.parseInt(String s);
    and you put your string between ().

    If you want to make a String from an Integer the quick way is:
    Java Code:
    String s= integer +"";

  3. #3
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    I changed them to temps[0] = Integer.parseInt(jan.substring(i).trim()); and now that seems somewhat fine. What i wanted to do there is convert the substring of a txt file, that i posted into an int. It would be the first number in the .txt file for each month. My problem is making it just that first number, and then im still stuck not knowing how to pull out the second number in the line. i.e. how would you pull two numbers off a string, that is a line of a txt file, when you would not know how long the line will be, or how or low the number will be. most likely the temps wouldnt be over 150 or below -50. Its getting them out of the string and then getting each one seperated

  4. #4
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    You text file display lines and then 3 words month, temp1 and temp2 that are separate by a space " ".

    So first you can put all lines in an Array of String[]. you can do with a new BufferedReader(new FileReader(txt-file)) and then with the method readline().

    Second you do a loop of this Array of String[] and you do a split(" ") on each line. You get for each line an String[] array of three words.
    Last edited by bigjo; 11-20-2011 at 05:52 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help with my school assignment, please

    No need for an array. Just simply use a Scanner object to read each line of the file while a line is available (using Scanner's hasNextLine() method), and then inside of this loop, create another Scanner object to read next() for the month, and nextInt() twice for both temps, and then display the results inside of the loop.

  6. #6
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    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.
    Java Code:
    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;
    	}
    
    }

  7. #7
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    How do i do the second part you did i can get the data into an array easily, its splitting up this data that im having trouble with.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: I need help with my school assignment, please


  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help with my school assignment, please

    Thanks for that Junky. Since he's likely getting this answered elsewhere, perhaps we should leave this one alone.

  10. #10
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    I didn't know how active these two sites were, so i posted on both. bigjo gave me the most help so far, i just need to figure out how to seperate both Tempuratures from each string.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: I need help with my school assignment, please

    Actually Fubar gave you the best advice: use Scanner.

  12. #12
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    I think i have it, i realize how i can do it, it is a bit of a mixture of the two, if i get it to work, i shall reply back.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: I need help with my school assignment, please

    JUST USE SCANNER

    Why would you need a mixture of the two?

  14. #14
    burnenator is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: I need help with my school assignment, please

    I solved my problem and got the program to work as i would like it. Thanks for the help. Sorry I was confusing with my problem. Your input was none the less, invaluable.

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2009, 03:41 PM
  2. Need help with school assignment
    By sljkbn in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 01:07 PM
  3. Help with noob school assignment
    By debo3381 in forum New To Java
    Replies: 3
    Last Post: 09-23-2009, 04:02 AM
  4. Question about school assignment
    By wata in forum New To Java
    Replies: 7
    Last Post: 08-18-2009, 02:00 PM
  5. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •