Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java Opening files and performing file output // Flowers.java - This program rea

ID: 3836348 • Letter: J

Question

java Opening files and performing file output

// Flowers.java - This program reads names of flowers and whether they are grown in shade or sun from an input
// file and prints the information to the user's screen.
// Input: flowers.dat.
// Output: Names of flowers and the words sun or shade.

the file it is supposed to use:

Astilbe
Shade
Marigold
Sun
Begonia
Sun
Primrose
Shade
Cosmos
Sun
Dahlia
Sun
Geranium
Sun
Foxglove
Shade
Trillium
Shade
Pansy
Sun
Petunia
Sun
Daisy
Sun
Aster
Sun

import java.io.*; // Import class for file input.

public class Flowers
{
public static void main(String args[]) throws Exception
{
String flowerNames;
String sun_or_shade;
// Declare variables here
FileReader fr = new FileReader("flowers.dat");
BufferedReader br = new BufferedReader(fr);
  
// Write while loop that reads records from file.
while((flowerNames = br.readLine()) != null)
{
sun_or_shade = br.readLine();
  
println(flowerNames + sun_or_shade);
}
// Print flower name and the words sun or shade.

br.close();
System.exit(0);
} // End of main() method.

} // End of Flowers class.

I have this program and I believe I am on the right track, but it will not run. the error it states is that it could not load or find the main class Flowers. Can you look at it and tell me what is wrong, and maybe explain the syntax behind it?

Another error I get is on the println(flowerNames + sun_or_shade) line, it says "cannot find symbol"

UPDATE: I got it, I foolishly typed println instead of System.out.println. Program runs fine now

Explanation / Answer

flowers.dat (Save the file under D Drive.then the path of the file pointing to it is D:\flowers.dat )

Astilbe
Shade
Marigold
Sun
Begonia
Sun
Primrose
Shade
Cosmos
Sun
Dahlia
Sun
Geranium
Sun
Foxglove
Shade
Trillium
Shade
Pansy
Sun
Petunia
Sun
Daisy
Sun
Aster
Sun

________________

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Flowers{

   public static void main(String[] args) {
try {
   //Opening the file
           Scanner ifsInput = new Scanner(new File("D:\flowers.dat"));
          
           //reading the data from the file
while(ifsInput.hasNextLine())
{
   //Displaying the flower name and the words sun or shade
   System.out.println(ifsInput.next()+" , "+ifsInput.next());
}
       } catch (FileNotFoundException e) {
           //Displaying the exception
           System.out.println(e);
       }

   }

}

______________

output:

Astilbe , Shade
Marigold , Sun
Begonia , Sun
Primrose , Shade
Cosmos , Sun
Dahlia , Sun
Geranium , Sun
Foxglove , Shade
Trillium , Shade
Pansy , Sun
Petunia , Sun
Daisy , Sun
Aster , Sun

________________Thank You