Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default monthly temperatures

    I have to create a program that will get data from a file, input it, get values from it, then output in the output window, as well as another new file. the input style is in this format:

    January, 72 -12
    February, 75 -5
    and so on...

    What would be the best method to get each of these numbers? I have to get average, min and max for each month and the year, as well as the average and range. I was thinking of removing the text before the comma, so I just have the two numbers on each line. i am not sure exactly how to use the comma delimiter, but I know it would definitely help here.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    I think that will be best solve this use some database. You don't need make some methods for parsing data ).
    Skype: petrarsentev
    http://TrackStudio.com

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

    Default

    This sounds like homework, so database is probably out. I would check the methods of the String class as you'll find several that will help you parse your Strings, then give it a go. The best way to do this is your way, if we can get it to work.

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    hm, i'm not too familiar with the api yet... so i would have to parse it somehow?

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

    Default

    Quote Originally Posted by droidus View Post
    hm, i'm not too familiar with the api yet... so i would have to parse it somehow?
    Yes, you would. There are simple methods in String that will allow you to find where the comma and dash are located, and that gives you the length of the String, and you can use these numbers with the subString method to extract your information. Check the String API, and you'll see what I'm talking about.

    Alternatively, you could split your String into an array of String tokens using the split method, but this would require a slightly complex regular expression parameter for it to work, and I think is not what you should be worrying about for an introductory course in Java.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    hm, split? only problem is, we haven't learned about it yet. we also haven't gotten to arrays yet... that is the next chapter (which we skipped over for reading/writing text files...

    here's what i go (which works):
    Java Code:
    		String[] temp;
    		String delimiter = ",";
    		
    		temp = data.split(delimiter);
    		
    		for(int i =0; i < temp.length ; i++)
    		testData = testData + "\n" + temp[i];
    		System.out.println(testData);
    output...

    January
    72 -12
    February
    75 -5
    and so on...

    now i guess my next step would be to get the numbers... and i guess my first step for this would be to see the numbers on every other line, add them up, and divide by two to get the average for each month.
    Last edited by droidus; 04-02-2011 at 03:16 PM.

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

    Default

    Quote Originally Posted by droidus View Post
    hm, split? only problem is, we haven't learned about it yet.

    we also haven't gotten to arrays yet... that is the next chapter (which we skipped over for reading/writing text files...
    Again, as I said, don't use split.

    Also, you would want to work with one line at a time, rather than the whole data. I recommend the following (in pseudocode):


    Java Code:
    for each line in the file
       get the current line and put it in a String variable, line
       Use String method to find location of "-" in line
       Use String method to get length of line
       Use these numbers to get the subString representing the number
       Convert number to int via Integer.parseInt(...)
       analyse number returned
    end for each line
    Last edited by Fubarable; 04-02-2011 at 03:21 PM.

  8. #8
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    i have the following code to see if there is another line:

    Java Code:
    	if (in.hasNextLine()){
    			System.out.println("true");
    		}
    		else{
    		System.out.println("false");
    		}
    and the input file with several lines of code. i get false. for my in variable:

    Java Code:
    Scanner in = new Scanner(inputFile); // Reads file contents
    i enter a file on my desktop. why doesn't this work properly?

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

    Default

    Quote Originally Posted by Fubarable View Post
    Use String method to find location of "-" in line
    [nitpick]
    Not all min temps will be in the negative.
    [/nitpick]

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

    Default

    Quote Originally Posted by droidus View Post
    Java Code:
    Scanner in = new Scanner(inputFile); // Reads file contents
    i enter a file on my desktop. why doesn't this work properly?
    This requires your file to be in the working directory and not on the desktop. I imagine you should be getting a FileNotFoundException unless you are swallowing it.

  11. #11
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    [nitpick]
    Not all min temps will be in the negative.
    [/nitpick]
    agreed. couldn't i just check to see if one number is greater than the other?

    and here is what i have so far:

    Java Code:
    		String inputFileName;
    		String outputFile;
    		String outputFileName;
    		String data;
    		int numbers = 0;
    		String months = "";
    		
    		//Prompts user for the input file location
          Scanner console = new Scanner(System.in);
    		System.out.println("Input file location: ");
          inputFileName = console.next();
    		//Tells the user for the output file location
    		System.out.println("The output file will be called: averageTemperatures.txt");
    		outputFileName = "averageTemperatures.txt";
    		
    		File inputFile = new File(inputFileName); // Get the input file
    		Scanner in = new Scanner(inputFile); // Reads file contents
    		PrintWriter out = new PrintWriter(outputFileName); // Selects the file to have the data 
    written to
    				
    		data = line(in);
    		System.out.println("This document reports the following statistics: \n" + data);
    
    		out.println(data);
    				
    		/*
    		months = months(data);
    		System.out.println(months);
    		*/
    		
    		String[] temp;
    		String delimiter = ",";
    		
    		temp = data.split(delimiter);
    		
    		String testData = "";
    		
    		for (int i =0; i < temp.length ; i++)
    		testData = testData + "\n" + temp[i];
    		System.out.println(testData);
    		
    		if (in.hasNextLine()){
    			System.out.println("true");
    		}
    		else{
    		System.out.println("false"); // [B]i get false here......[/B]
    		}
    		
    		while (in.hasNextLine()){  // [B]but this does not work....[/B]
    			
    			String lines = "";
    			String line = in.nextLine();
    			
    			lines = line + lines;
    			
    			System.out.print("Data: " + lines);
    		}
    							
    		in.close();
          out.close();
    		
    	}		
    		
    		
    		// This method gets the data
    		public static String line(Scanner in){
    		
    			String line = "";
    			String data = "";
    			
    			while (in.hasNextLine()){ // [B]this works.....[/B]
    			
    				line = in.nextLine();
    				data = data + "\n" + line;
    
    			}
    			
    			return data;
    		
    		}
    it sounds like you can use the scanner in once, then you can't use it anymore.... i tried my line method, copied it exactly, and i could not get it to print out... so is it true that you can only use it once?

    update:

    so i added: "Scanner ins = new Scanner(inputFile);" to my program, and used it in my method. it worked!!!! ahhh so confused though! why can you only use a scanner once???? and why can't i use in.hasNextline on a variable such as testData, since my data will be in the format of:

    January
    55 24
    February
    67 29

    ?? i guess you can only use scanners with it?
    Last edited by droidus; 04-04-2011 at 02:01 AM.

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

    Default

    Quote Originally Posted by droidus View Post
    couldn't i just check to see if one number is greater than the other?
    Why? Your data is in the form: Month, max temp min temp. Simply read the line and extract the 3 bits of data.
    why can you only use a scanner once????
    Depends upon what you mean by once. You can continue to read data from the Scanner as long as there is data left. If you use a Scanner to read an entire file then there is no data left. If you want to read the file again then you need to create a new Scanner. I don't know if there is a way to get the Scanner to go back to the start (probably not).

  13. #13
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    hm, ok. i would have never guessed when it reads the file, that it actually takes the content... im not exactly sure how to take each line from the file, and set each one to a string. i can print the whole file out... i want to read every line of testData, but it will only work with a scanner. how else could i do this?
    Last edited by droidus; 04-04-2011 at 07:13 PM.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default

    I don't know if there is a way to get the Scanner to go back to the start (probably not).
    You're right, there isn't.

    db

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

    Default

    Quote Originally Posted by droidus View Post
    i would have never guessed when it reads the file, that it actually takes the content
    No, it doesn't "take" the contents. The file will remain unchanged. What the Scanner does is similar to using your finger while you read a book. You place your finger on the first line of the page, then as you read you move your finger down the page. In the case of the Scanner there is no way to move your finger backwards, only forwards until you have finished the book.

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

    Default

    Quote Originally Posted by droidus View Post
    im not exactly sure how to take each line from the file, and set each one to a string.
    Use the Scanner to read a single line from the file. You then process this file, maybe create a class that has three instance variables (month, max, min). Create an object from the data read. Insert object into an array or better a List. Repeat until entire file is read.

  17. #17
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    getting into arrays in the next chapter... grrrr can't wait!!

    i have the following:

    Java Code:
    public static String temperatures(String testData, Scanner ins){ // yes, ins...
    		
    			String curNum = "";
    			int i = 0;
    			char ch = ' ';
    			char num = ' ';
    			String nums = "";
    		
    			for (i=0; i<1; i++){
    			
    				ch = testData.charAt(i);
    			
    				if (ins.hasNextLine()){
    					curNum = (ins.nextLine());
    				}
    					
    			}
    			
    			return curNum;
    			
    		}
    but my scanner ins data is all on one line. on testData, i have it in the following format:

    January
    72 -12
    February
    75 -5
    March
    80 8
    and so on....

    but i can't use testData.hasNextLine() since this variable is not a scanner... how would i go about doing this?

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

    Default

    <confused>

    In your first post data was all on one line. Now you say data is spread over 2 lines. How are we supposed to help if you provided incomplete and/or incorrect information. Are you freading a line of data from the file in another method and then passing it to the temperature method? Or is the temperature method supposed to readd all lines from the file?

    Step away from the computer, clear your head, gather your thoughts and provide us with the relevant information that will allow us to help you.

  19. #19
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    i am now going to start with testData. this is where i will start. it is in the format as mentioned above. my one question is, how do you read the next line (just like with scanner) of testData (a string, with the data formatted as above)?

    also, if i print this testData to a file with out.print(testData);, why does it leave it in the following format?:
    Attached Thumbnails Attached Thumbnails monthly temperatures-java.jpg  
    Last edited by droidus; 04-05-2011 at 01:47 AM.

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

    Default

    You still are not making sense. If you data is spread over 2 lines then you need to read 2 lines from the file.
    Java Code:
    loop over file {
        month = read line from file
        temps = read line from file
        split temps
        convert temps
        do other stuff
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. My code for converting temperatures...
    By king4ever in forum New To Java
    Replies: 8
    Last Post: 01-18-2011, 04:27 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
  •