Question

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 = head;

       Node fast_ptr = head;

       if (head != null)

       {

           while (fast_ptr != null && fast_ptr.next != null)

           {

               fast_ptr = fast_ptr.next.next;

               slow_ptr = slow_ptr.next;

           }

           System.out.println("The middle element is [" +

                               slow_ptr.data + "] \n");

       }

   }

   public void push(int new_data)

   {

       Node new_node = new Node(new_data);

       new_node.next = head;

       head = new_node;

   }

   public void printList()

   {

       Node tnode = head;

       while (tnode != null)

       {

           System.out.print(tnode.data+"->");

           tnode = tnode.next;

       }

       System.out.println("NULL");

   }

   public class TextFileReadingAndWriting {

    public static void main(String[] args) {

        try {

            FileReader reader = new FileReader("MyFile.txt");

try {

            FileWriter writer = new FileWriter("MyFile.txt", true);

            BufferedReader bufferedReader = new BufferedReader(reader);

            String line;

            while ((line = bufferedReader.readLine()) != null) {

                System.out.println(line);

            }

            reader.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

this is string list in a text file which I like to read:

Hishaam Esteban Kevin

Matthew    Brandon

Joel Luis     Jianwei Yechiel

Taeyong

Jiayu

Jiade Phillip

Russell         Mohebullah

Akshar   Evgeniia       Andres   Marco Justin

Robin Kelvin Zhiheng Jeffrey

Yifei Yinyu

Jiaxin Youyia Eleftherios

Yuan

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

CODE

import java.io.*;

class LinkedList {

    Node head;

    class Node {

        int data;

        Node next;

        Node(int d) {

            data = d;

            next = null;

        }

    }

    void printMiddle() {

        Node slow_ptr = head;

        Node fast_ptr = head;

        if (head != null) {

            while (fast_ptr != null && fast_ptr.next != null) {

                fast_ptr = fast_ptr.next.next;

                slow_ptr = slow_ptr.next;

            }

            System.out.println("The middle element is [" +

                    slow_ptr.data + "] \n");

        }

    }

    public void push(int new_data) {

        Node new_node = new Node(new_data);

        new_node.next = head;

        head = new_node;

    }

    public void printList() {

        Node tnode = head;

        while (tnode != null) {

            System.out.print(tnode.data + "->");

            tnode = tnode.next;

        }

        System.out.println("NULL");

    }
}

public class TextFileReadingAndWriting {

    public static void main(String[] args) {

        try {

            FileReader reader = new FileReader("MyFile.txt");

            try {

                FileWriter writer = new FileWriter("MyFile.txt", true);

                BufferedReader bufferedReader = new BufferedReader(reader);

                String line;

                while ((line = bufferedReader.readLine()) != null) {

                    System.out.println(line);

                }

                reader.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Please help me fix my errors. I would like to read and write the text file...
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 need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

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

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

  • How to solve this problem by using reverse String and Binary search? Read the input one...

    How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

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

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

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