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

View all comments

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();
    }
}