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

Show parent comments

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 :(

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.