Question

Having trouble with my code, it keeps giving me an error every time I run it....

Having trouble with my code, it keeps giving me an error every time I run it. Can someone please help me understand what I'm doing wrong?

*********CODE***************

// Java program to read the file contents, sort it and output the sorted content to another file
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class Datasort {

public static void main(String[] args) throws IOException {
File fin = new File("input.txt"); // input file
File fout = new File("sorted_input.txt"); // create an out file
// Java FileInputStream class obtains input bytes from a file
FileInputStream fis = new FileInputStream(fin);   
// buffering characters so as to provide for the efficient reading of characters, arrays, and lines
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
// declare an array in-line, ready for the sort
String aLine;
ArrayList<String> al = new ArrayList<String> ();
int i = 0;
while ((aLine = in.readLine()) != null) {
al.add(aLine);   
}
// set the sort for values is greater than 0
Collections.sort(al); // sorted content to the output file
// writer object of BufferedWriter class to write to the output file
BufferedWriter writer = new BufferedWriter(new FileWriter(fout));
// loop to write the sorted contents to output file
for(i=0;i<al.size();i++)
{
if(i < al.size()-1)
writer.write(al.get(i)+"\n");
else // last line shouldn't end with a new line
writer.write(al.get(i));

}
// close the 2 files   
writer.close();
in.close();
}

}

//end of program

ERROR:

run:
Error: Could not find or load main class datasort.Datasort
Caused by: java.lang.ClassNotFoundException: datasort.Datasort
C:\Users\jonni\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\jonni\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)

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

//The program working is fine without any issues:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class Datasort {

   public static void main(String[] args) throws IOException {
       File fin = new File("input.txt"); // input file
       File fout = new File("sorted_input.txt"); // create an out file
// Java FileInputStream class obtains input bytes from a file
       FileInputStream fis = new FileInputStream(fin);
// buffering characters so as to provide for the efficient reading of characters, arrays, and lines
       BufferedReader in = new BufferedReader(new InputStreamReader(fis));
// declare an array in-line, ready for the sort
       String aLine;
       ArrayList<String> al = new ArrayList<String>();
       int i = 0;
       while ((aLine = in.readLine()) != null) {
           al.add(aLine);
       }
// set the sort for values is greater than 0
       Collections.sort(al); // sorted content to the output file
// writer object of BufferedWriter class to write to the output file
       BufferedWriter writer = new BufferedWriter(new FileWriter(fout));
// loop to write the sorted contents to output file
       for (i = 0; i < al.size(); i++) {
           if (i < al.size() - 1)
               writer.write(al.get(i) + "\n");
           else // last line shouldn't end with a new line
               writer.write(al.get(i));

       }
// close the 2 files   
       writer.close();
       in.close();
   }

}

Please follow the below instructions:

1) copy the code to notepad

2) delete the file from the netbeans

3)create new file with name Datasort.java

4)copy code from notepad and paste

5) Run the program

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Having trouble with my code, it keeps giving me an error every time I run it....
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
  • Can someone please help me? I'm having trouble with this assignment and have been stuck trying...

    Can someone please help me? I'm having trouble with this assignment and have been stuck trying to figure it out for over an hour. Here's the assignment details below: For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents...

  • import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private...

    import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private static final String OUTPUT_FILE = "crimes.txt"; public static void main(String[] args) { int totalCrimes = 0; // read all the rows from the csv file and add the total count in the totalCrimes variable try { Scanner fileScanner = new Scanner(new File(INPUT_FILE)); String line = fileScanner.nextLine(); // skip first line while (fileScanner.hasNext()) { String[] tokens = fileScanner.nextLine().split(","); if (tokens.length == 4) { totalCrimes +=...

  • 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 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...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

  • Hello Can you help to fix the program. When running, it still show Exception in thread...

    Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap    at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

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