Question: I can't find the mistake in the program import java.io.*; import java.util.*; class WeatherReporter { //main() begins public static void main(String[] args) { //Scanner class

I can't find the mistake in the program

import java.io.*;

import java.util.*;

class WeatherReporter

{

//main() begins

public static void main(String[] args)

{

//Scanner class object to read from console

Scanner scan = new Scanner(System.in);

//Input file name

System.out.println("Enter the input file name");

String fileName = scan.nextLine();

//File class object with file name as fileName

File file = new File(fileName);

//scan = new Scanner(file); can throw FileNotFoundException if the specified file is not at the location

try

{

//Scanner class object to read from file

scan = new Scanner(file);

//Loop runs until there are no more lines in the file

while (scan.hasNextLine())

//Read a line from the file and pass it as parameter to processLine() method

processLine(scan.nextLine());

}

//Catches FileNotFoundException catch (FileNotFoundException e)

{

e.printStackTrace();

}

}

//main() ends

//Function to print the line in the given format which takes a string as parameter

public static void processLine(String line)

{

//Variable to store the indices of spaces in the line

int space1 = line.indexOf(" "); //Returns the index of 1st occurrence of " " from index 0

int space2 = line.indexOf(" ", space1 + 1); //Returns the index of 1st occurrence of " " from index space1 + 1

int space3 = line.indexOf(" ", space2 + 1); //Returns the index of 1st occurrence of " " from index space2 + 1

int space4 = line.indexOf(" ", space3 + 1); //Returns the index of 1st occurrence of " " from index space3 + 1

//Print the date

//Month is from index 4 to 5, date is from index 6 to 7 and year is from index 0 to 3

//line.substring(i, j); returns a substring out of line string from index i to j - 1

System.out.print(line.substring(4, 6) + "/" + line.substring(6, 8) + "/" + line.substring(0, 4));

//Print lowest temperature

//low stores the lowest temperature which is from index space3 + 1 to space4 - 1

//String returned from substring() is converted to float using Float.parseFloat()

float low = Float.parseFloat(line.substring(space3 + 1, space4));

System.out.print(" Low: " + low);

//Print highest temperature

//high stores the highest temperature which is from index space2 + 1 to space3 - 1

//String returned from substring() is converted to float using Float.parseFloat()

float high = Float.parseFloat(line.substring(space2 + 1, space3));

System.out.print(" High: " + high);

//Print rain

//rain stores "no" if the 5th last character in the line is '0', otherwise, "yes"

String rain = line.charAt(line.length() - 5) == '0' ? "no" : "yes";

System.out.print(" Rain: " + rain);

//Print snow

//snow stores "no" if the 4th last character in the line is '0', otherwise, "yes"

String snow = line.charAt(line.length() - 4) == '0' ? "no" : "yes";

System.out.print(" Snow: " + snow);

System.out.println();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!