Question

I'm working with Java and I have a error message  1 error Error: Could not find or...

I'm working with Java and I have a error message  1 error
Error: Could not find or load main class Flowers. This is the problem:

Instructions

  1. Make sure the source code file named Flowers.java is open.
  2. Declare the variables you will need.
  3. Write the Java statements that will open the input file, flowers.dat, for reading.
  4. Write a while loop to read the input until EOF is reached.
  5. In the body of the loop, print the name of each flower and where it can be grown (sun or shade) using a format of flowerName + " is grown in the " + sunOrShade).
  6. Execute the program

This is what I have so far:

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

public class Flowers
{
public static void main(String args[]) throws Exception
{
// Declare variables here
String flowerName;
String sunOrShade;
  
// Open input file.
FileReader fr = new FileReader("Flowers.dat");
  
// Create BufferedReader object.
BufferedReader br = new BufferedReader (fr);

// Write while loop that reads records from file.
while ((flowerName=br.readLine()) !=null)

// Print flower name and the words sun or shade.
System.out.println("Flowers: " + flowerName + "\nGrown in: " + sunOrShade);

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

} // End of Flowers class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'Could not find or load main class Flowers' error occurs when there is no Compiled class Flowers.class is present in the Classpath.

First, we need to compile the class using the command

javac Flowers.java

Once succesfully compiled, we can see Flowers.class file in the same folder.

Then we need to run the program using the command

java Flowers

It should produce the expected output.

CODE :

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

public class Flowers
{
public static void main(String args[]) throws Exception
{
// Declare variables here
String flowerName;
String sunOrShade;

// Open input file.
FileReader fr = new FileReader("Flowers.dat");

// Create BufferedReader object.
BufferedReader br = new BufferedReader (fr);

// Write while loop that reads records from file.
String input;
while ((input=br.readLine()) !=null) {

// Print flower name and the words sun or shade.
String words[] = input.split(" ");
flowerName = words[0];
sunOrShade = words[1];
System.out.println("Flowers: " + flowerName + "\nGrown in: " + sunOrShade);
}
br.close();
System.exit(0);
} // End of main() method.

} // End of Flowers class.

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
I'm working with Java and I have a error message  1 error Error: Could not find or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have my assignment printing, but I can find why the flowers is printing null and...

    I have my assignment printing, but I can find why the flowers is printing null and not the type of flower. Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where it can be...

  • How do I complete this problem? * CENGAGE MINDIAP > Terminal Opening Files and Performing FileInput...

    How do I complete this problem? * CENGAGE MINDIAP > Terminal Opening Files and Performing FileInput in C++ 0 Opening Files and Performing File Input Flowers.cpp flowers.dat 1 // Flowers.cpp - This program reads names of flowers and whether they are grown in shade or sun from an input 2 // file and prints the information to the user's screen. 3.// Tnput: flowers.dat. 4 // Output: Names of flowers and the words sun or shade. Summary In this lab, you...

  • package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...

    package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public static void main (String [] args)    {    // ============================================================    // Step 2. Declaring Variables You Need    // These constants are used to define 2D array and loop conditions    final int NUM_ROWS = 4;    final int NUM_COLS = 3;            String filename = "Input.txt";    // A String variable used to save the lines read from input...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • Opening Files and Performing File Input 1 V/ Flowers.cpp This program reads nanes of flowers and ...

    Opening Files and Performing File Input in C++ Opening Files and Performing File Input 1 V/ Flowers.cpp This program reads nanes of flowers and whether they are grown in shade or sun from an input 2 file and prints the information to the user's screen. 3 Input: flowers.dat 41 Output: Names of flowers and the words sun or shade Summary In this lab, you open a file and read input from that file in a prewritten C++ program. The program...

  • What is the problem with my Program ? also I need a Jframe that desplays the...

    What is the problem with my Program ? also I need a Jframe that desplays the original input on the left side and the sorted input of the left side. my program is supose to read the number for basketball players, first name, last name, and float number that is less than 0 . on the left sideit is supposed to sort all this input based on last name. This is my demo class : package baseball; import java.io.*; import...

  • Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import...

    Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class myData { public static void main(String[] args) { String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (‘stop’ to quit)."); try (FileWriter fw = new FileWriter("test.txt")) { do { System.out.print(": "); str = br.readLine(); if (str.compareTo("stop") == 0) break; str = str + "\r\n"; // add newline fw.write(str); } while (str.compareTo("stop") != 0); } catch (IOException...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

  • In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile...

    In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile () class. You are provided another class called Finally you are provided the file you must read from in the MyAbstract class. You will need to extend MyAbstract to MyTestClass. You should have the following outputs. The content of myData employee avg salary number of employees File that was created in myFile() Everything I was provided is listed already. l. Servers ary 1 35...

  • i need help with this error and what i need to fix exactly . File Edit...

    i need help with this error and what i need to fix exactly . File Edit View Build Project Settings Tools Window Help MEDIKUAR 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 private static double[] readDataPrompile() throws IOException { double[] rainfallDataFromFile - new double[12]; 7/File file - new File("/Users/destinytaylor/DTaylor Lab 4/rainfallData.txt"); //kept file in same directo File file = new File("rainfalldata.txt"); BufferedReader br new BufferedReader(new FileReader(file)); iedene String data; int i...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT