r/javahelp • • Nov 06 '19

Going crazy, need help with a program

Hi guys, I'm an ansipring java developer. My developer friend gave me an exercise and I can't for the life of me figure out how to do it. If you guys could help I would be so grateful.

This is the exercise:

Given a weather.dat file, write a program that given the file in input returns the day with the biggest and lowest temperature range. In this file there are 30 days and there's a lot of data for these days, but the temperature columns are the second and third one. So it's like : Day | Max Temp | Lowest Temp| ecc | ecc | ...

I understood I have to use Java Arrays and I managed to have the program read all the file but I don't know how to proceed. Help, thanks lol

16 Upvotes

13 comments sorted by

5

u/mikelak Nov 06 '19

where exactly are you stuck?

did you read all the file as one string or one line after the other?

did you manage to fill the arraylist?

and how did you approach getting the range? two lists or 1?

2

u/[deleted] Nov 06 '19

One line after the other. How can I make it read only the first 3 columns which are day, max temp and lowest temp? For the end of it I only want to display the day with the highest temperature range and the day with the lowest.

2

u/mikelak Nov 06 '19

one line after the other is better than only the first 3 columns.

to get the first 3 columns you should think about splitting the string you have in hand using the delimited character of the line. once that is done you will have an array of strings, once you reach this stage you can find the indexes of the day, minTemp and maxTemp to manipulate them as you want. if you find any more trouble ask please

1

u/[deleted] Nov 06 '19

Thank you very much!! So this is where I'm at for now

import java.io*; class Weather { public static void main(String args[]) throws IOException { FileReader f; f=new FileReader("Weather.dat");

BufferedReader b;
b=new BufferedReader(f);

StringWriter query = new StringWriter();
query.append(" SELECT dy, mxt, mnt FROM Weather.dat ");
query.append(" WHERE mxt - mnt >= 54 ");

StringWriter query = new StringWriter();
query.append(" SELECT dy, mxt, mnt FROM Weather.dat ");
query.append(" WHERE MXT - MNT <= 13 ");

logger.info 

And I don't know how to move on :(

1

u/wildjokers Nov 06 '19

I have no idea why you are using SQL, you aren't querying a database.

Instead read each line in and then the String class has a split method:

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#split(java.lang.String)

You will need to split on the pipe character (|). That will give you an array of fields, you want field 0, 1, and 2 from the array.

1

u/[deleted] Nov 07 '19

Yeah I knew sql querys were wrong, but still decided to try them because sql databases is where I'm at my most comfortable. Thank you, I did that, I split the string on the spaces between the data.

3

u/mikelak Nov 06 '19
  1. don't make the main method throw an exception, try catch around the file reader for better error handling.

  2. why are you using queries?

  3. you can read the file line by line, loop through the lines an print them as first step, show the code when you reach this stage. (i would suggest continuing in private messages)

2

u/msx Nov 06 '19

uh what would those sql queries do there? :) there's no database, you're just reading a static file. You have your BufferedReader b that can give you each line from the file (look here and see if you can set up a loop that iterate all the lines).

Try to decompose the problem in smaller steps. Something like this:

  • read lines from file

  • break lines into tokens (perhaps using | as the separator if i undestood completely)

  • from those tokens, extract the second and third, transform them into integer (or floats, dunno)

  • compare them

  • etc.

1

u/subsonic87 Nov 06 '19

Check the sidebar rules:

No "Do my homework" posts! Do not ask for complete solutions. Tell us: What have you tried?

Can you explain what you've tried, and where you're at?

1

u/[deleted] Nov 07 '19

I was not asking to "do my homework". I explained where I'm at and the fact that I do not know how to proceed. Wasn't expecting a solve, but just some suggestions. This is where I'm at right now.

package Weather;

import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream;

public class Weather { public static void main(String[] args) { String filePath = "C:/Users/MyName/Documents/Workspace_Weather/src/weather.dat"; String[] splitStr = filePath.trim().split("\s+");

        System.out.println( readLineByLineJava8( splitStr ) );
    }
 
    private static String readLineByLineJava8(String[] splitStr) 
    {
        StringBuilder contentBuilder = new StringBuilder();
 
        try (Stream<String> stream = Files.lines( Paths.get(null, splitStr), StandardCharsets.UTF_8)) 
        {
            stream.forEach(s -> contentBuilder.append(s).append("\n"));
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
 
        return contentBuilder.toString();
    }
}

1

u/juckele Barista Nov 06 '19

First, it's great that you've gotten as far as you have. I've had a lot of people ask me to teach them to program, and I can tell them to download a specific program, open it, and report back when they've done that. That step actually dissuades most people, so that you've written some code in a serious attempt to complete the task is fantastic!

That said, I honestly think you'd benefit from a slightly more formal introduction than "read this file and find the min and max values". The problem is that this is assuming you know how to work with arrays, loops, and logical statements all at once, and it's clear from your code that you don't have all of these skills yet. Practicing them all at once is leaving you lost, and I doubt it's a great approach for you.

I'd recommend that you grab a book that does a more structured introduction. I really like this one, which is free online, and structured really well: https://greenteapress.com/wp/think-java/

Go through the exercises at the end of each chapter, feel free to post here if anything stumps you, skip the grid world stuff.

1

u/techtamashi Nov 06 '19
  1. Read the file line by line

  2. Save each line to a String

  3. Java has a String Split method that can split a String to an array of Strings based on the character(s) you want to split on - in this case, you would split based on the | character

  4. Next you would convert the values in the 2nd and 3rd places to Integers or Floating point numbers as appropriate and calculate the difference between them. Save the differences to a Map of Day and Range (i.e. difference).

  5. Finally iterate over the Map and identify which Day has the min and max difference - which represent the days with the lowest and highest temperature ranges.