-1

I'm supposed to be coding an app that can read names from a hardcoded text file, save them as a string array, then write those names in a different text file but sorted. I believe I have the first two parts down but I'm confused on how to sort the names then write them into a new file.

These is the actual problem I'm working on:

"Take an input file with 10 names in it (hard coded). Write a program to read the file, save the names in a String array and write into a different file names in sorted order. Use Methods appropriately."

BTW I'm a rookie coder, this is what I have so far.

public static void main(String[] args) throws FileNotFoundException {
    // TODO code application logic here
    readFile();
    saveStringArray();
}

public static void readFile() {
    File file = new File("/Users/nicoladaaboul/Desktop/Programming/C++, "
        + "HTML5, Java, PHP/Java/Question2/names.txt");
    try {
        Scanner sc = new Scanner(file);
        while (sc.hasNextLine()) {
            String i = sc.next();
        }
        sc.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public static void saveStringArray() throws FileNotFoundException {
    String token1 = "";
    Scanner inFile1 = new Scanner(new File("names.txt")).useDelimiter(",\\s*");
    List<String> temps = new ArrayList<String>();

    while (inFile1.hasNext()) {
        token1 = inFile1.next();
        temps.add(token1);
    }

    inFile1.close();
    String[] tempsArray = temps.toArray(new String[0]);
    Arrays.sort(tempsArray);

    for (String s : tempsArray) {
        System.out.println(s);
    }
}

public static void sortingNames() {
}

public static void writingFile() throws FileNotFoundException {
    PrintWriter writer = new PrintWriter("sortedNames.txt");
    writer.close();
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Nicola Daaboul
  • 77
  • 2
  • 2
  • 6
  • You should show some effort (code) when asking a question. – Mick Mnemonic May 02 '15 at 00:04
  • It's not clear what your question is. If you can ask a specific question, you'll get a specific answer. But it's not clear what you can and can't do, and which bit you're stuck on. Or, if you're trying to get someone to write the code for you, there are other web sites where you can hire programming staff. – Dawood ibn Kareem May 02 '15 at 00:05
  • [Is this a homework question?](http://stackoverflow.com/help/on-topic) Can you be more specific as to what problems you're having with sorting and writing to a file? – Zsw May 02 '15 at 00:06

1 Answers1

0

Its important that you break your problem down into instructions. 1. You need to read the file you can use bufferedReader(code below). 2. Create an array(or arraylist) to store your string values. 3. Then as you read each line, store these values in the array. 4. When finished reading the file you then would pass this array to a function that would sort it(Why does my sorting loop seem to append an element where it shouldn't?). 5. Once sorted you simply write this array, to a file.

BufferedReader br = new BufferReader(new FileReader("name.txt"));
int count = 0;
String line;
String[] names = new String[100];
while((line = br.nextLine()) != null){
    names[count] = line;
    count++;
}
Community
  • 1
  • 1
Wally
  • 47
  • 7
  • So what you typed out is pretty much both the methods in one, it'll read it from the file while simultaneously storing it in a string array? – Nicola Daaboul May 02 '15 at 00:45