So basically I have to import a file of names and then be able to sort them alphabetically I've managed to import the file using the followig code

import java.io.*;
public class Name{
   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();
              System.out.println(contents);

          }
          fr.close();
      }
}

also I have managed to sort them alphabetically when manually entering the list of names as shown in the code below

public class DSA1
{
     public static void main(String[ ] args)
     {
             String[ ] names = {"James", "Anne", "Bill", "Maria", "Bob", "Jill", "Elvis", "Carol", "Dennis", "Mandy", "Steven", "Sian", "Harry", "Linda"};
             sortStringExchange (names);
             for ( int k = 0;  k < 14;  k++ )
                System.out.println( names [ k ] );
      }

      public static void sortStringExchange( String  x [ ] )
      {
            int i, j;
            String temp;

            for ( i = 0;  i < x.length - 1;  i++ )
            {
                for ( j = i + 1;  j < x.length;  j++ )
                {  
                         if ( x [ i ].compareToIgnoreCase( x [ j ] ) > 0 )
                          {                                             // ascending sort
                                      temp = x [ i ];
                                      x [ i ] = x [ j ];    // swapping
                                      x [ j ] = temp; 
                                      
                           } 
                   } 
             } 
      } 
}

I just can't seem to figure out how to combine the two pieces of code to make one program, all help will be much appreciated

The code that reads the file needs to save the Strings it reads in an array or arrayList so that they can be sorted after they are all read in.
The problem with an array is that it must be defined large enough to hold all the Strings you want to save. If you use an ArrayList, it will resize itself as needed.
There are ways to use an array but it can require some complication in the code or some assumptions on how many Strings you are going to read in.

ok so how do i go about implementing the array list in

i changed my code now it reads

/**
   * read a string and write in alphabetical order.
    */
    import java.io.*;
    import java.util.*;

    class Alpha {
  
    
    String alphaOrder(String str){
    char[] charArray = str.toCharArray();
    Arrays.sort(charArray);
    String aString = new String(charArray);
   return aString ;
    }

    public static void main(String[] args) throws IOException {
   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();}
   System.out.println("String before reverse : "names.txt");
      String alphaString = obj.alphaOrder(names.txt);
      System.out.println("String in alphabetical order : " + alphaString);
    }
   }

but it still doesnt work any ideas on how to change it so it works im totally confused

it still doesnt work

Please explain. Show what it does and add comments describing what you want it to do.

For example what do you do with the String you read into the contents variable?

well basically i want the program to read a list of names from the file named "names.txt" then to display them in the order they appear in the list then to display the list after it has been sorted alphabetically

Do it one step at a time. Leave the rest for later.
1)read the contents of a file and display the lines as they are read.
Nothing more. Read the lines and display them.

ok so i can do that part using

import java.io.*;
public class Fileread{
   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();
              System.out.println(contents);
          }
          fr.close();
      }
}

Now save the lines in a container so that they can be sorted. After you fill the container print out its contents.
An ArrayList would be a possible container to use.

ok so would i create and add to the array by using code such as

ArrayList<String> names = new ArrayList<String>(14);
names.add(contents);

for (int i = 0; i<names.size(); i++)
System.out.println(names.get(i));

something along these lines?

ArrayList<String> names = new ArrayList<String>(14);
names.add(contents);

for (int i = 0; i<names.size(); i++)
System.out.println(names.get(i));

What's next? For sorting the contents of a list see the Collections class's sort method.

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.