so heres my code

import java.awt.*;
import java.text.*;
import java.util.*;
import java.util.List; // Explicit import required
import javax.swing.*;

public class Sort {
 public static void main(String args[]) {
   Runnable runner = new Runnable() {
     public void run() {
       String words[] = {"Sean", "Beth", "Ryan",
                         "Bree", "Jim", "Bob"};
       List list = Arrays.asList(words);
       JFrame frame = new JFrame("Sorting");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
       Box box = Box.createVerticalBox();
       frame.setContentPane(box);
       JLabel label = new JLabel("Name List:");
       box.add(label);
       JTextArea textArea = new JTextArea( list.toString());
       box.add(textArea);
       Collections.sort(list);
       label = new JLabel("Sorted Name List:");
       box.add(label);
       textArea = new JTextArea(list.toString ());
       box.add(textArea);
       frame.setSize(400, 200);
       frame.setVisible(true);
     }
   };
   EventQueue.invokeLater (runner);
 }
}

Now instead of having the names entered in such as in this line of code

String words[] = {"Sean", "Beth", "Ryan",
                         "Bree", "Jim", "Bob"};

I want the names entered using a text file created in notepad, I know the file needs to be read into an array and all that Im just not sure how to replace the code e.g. where it goes in the code I already have

I wouldn't worry too much about an array, because all you do with that it turn it into a List on line 13.
For a clean solution you could write a separate method that reads the file one line at a time and adds each line to an ArrayList. then finally returns that ArrayList. Then all you need to do is to call that method on line 13 and assign its returned ArrayList to your List list.

so would the code be like

import java.awt.*;
import java.text.*;
import java.util.*;
import java.util.List; // Explicit import required
import javax.swing.*;
import java.io.*;

public class Name9 {
 public static void main(String[] args)throws IOException{
      String contents;
      File f = new File("names.txt");
      FileReader fr = new FileReader(f);
      BufferedReader br = new BufferedReader(fr);
         while (br.ready()){
              contents = br.readLine();
                        }
          fr.close();
      }
}

   Runnable runner = new Runnable() {
     public void run() {
       String contents[] = {contents};
       List list = Arrays.asList(contents);
       JFrame frame = new JFrame("Sorting");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
       Box box = Box.createVerticalBox();
       frame.setContentPane(box);
       JLabel label = new JLabel("Word List:");
       box.add(label);
       JTextArea textArea = new JTextArea( list.toString());
       box.add(textArea);
       Collections.sort(list);
       label = new JLabel("Sorted Word List:");
       box.add(label);
       textArea = new JTextArea(list.toString ());
       box.add(textArea);
       frame.setSize(400, 200);
       frame.setVisible(true);
     }
   };
   EventQueue.invokeLater (runner);
 }
}

but its coming up with errors but not sure what I've done wrong

its coming up with errors

Please copy and paste here the full text of the error messages.

im using jdk and cimmand prompt to run

error list its coming up with is

Name9.java:21: 'class' or 'interface' expected
        Runnable runner = new Runnable <> {
Name9.java:42: 'class' or 'interface' expected
        EventQueue.invokeLater <runner>;
Name9.java:43: 'class' or 'interface' expected
         {
Name9.java:45: 'class' or 'interface' expected

Check that the {}s are properly positioned and paired.
Is the code inside of a method?

ok so turned out i had 2 too many '}' so got rid of those but its still coming up with

Name9.java:21: 'class' or 'interface' expected
Runnable runner = new Runnable <> {

as an error not quite sure what this means

Is than code inside of a method?

no thats literally everything I have

Is the line of code causing the problem inside of {}s that define the boundary of a method?

I'm not quite sure of what your asking me, from what I can work out the error is on line 22 on of the code about not having a class or something but not sure what to do with it

class AClass {
  public void aMethod() {
     // here is inside the method's enclosing {}s
     // this is where code should go
  }  // end of method

  // here is outside of method 
  // code here will cause compiler error
} // end class

not quite sure what your asking me all {}s are there though

What code do you have following the end of the main() method?
That code should be in a method, not at the class level outside of any method.

I dont have any other code that's everything right there

Look at your code. Where is the } at the end of the main() method?
What statements are after that }?

Post your current code so the line numbers are shown to talk about.

import java.awt.*;
import java.text.*;
import java.util.*;
import java.util.List; // Explicit import required
import javax.swing.*;
import java.io.*;
 
public class Name9 {
 public static void main(String[] args)throws IOException{
      String contents;
      File f = new File("names.txt");
      FileReader fr = new FileReader(f);
      BufferedReader br = new BufferedReader(fr);
         while (br.ready()){
              contents = br.readLine();
                        }
          fr.close();
      }
}
 public class Sort {
   Runnable runner = new Runnable() {
     public void run() {
       String contents[] = {contents};
       List list = Arrays.asList(contents);
       JFrame frame = new JFrame("Sorting");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
       Box box = Box.createVerticalBox();
       frame.setContentPane(box);
       JLabel label = new JLabel("Word List:");
       box.add(label);
       JTextArea textArea = new JTextArea( list.toString());
       box.add(textArea);
       Collections.sort(list);
       label = new JLabel("Sorted Word List:");
       box.add(label);
       textArea = new JTextArea(list.toString ());
       box.add(textArea);
       frame.setSize(400, 200);
       frame.setVisible(true);
     
   };
   EventQueue.invokeLater (runner);
 }
}

that's the code as it currently is

You can not have two public classes in the same file.

The code where the error occurs in the Sort class is not inside of a method.

ok so does the second public class need to become a private class within the public class

well i changed it to private class but now getting an error message of

25: variable contents might not have been initialized
String contents [] = (contents);

and thats the only error showing but once again i'm clueless to what it means

Are you defining a variable that will contain itself???

????

The statement defines a variable named contents and then assigns to it the value of contents. How can that be done?

I don't know i'm just trying to read the contents from the file and display the names sorted but looks like its turning out to be an epic fail

Why not save what is being read from the file as it is being read in the loop in the main() method?
Read into contents and then save contents in an arraylist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.