Need help creating an array that reads and writes a .txt and lists words in alphabetical orders

StackOverflow https://stackoverflow.com/questions/19505617

  •  01-07-2022
  •  | 
  •  

質問

I just started to code a while back and I'm in the process of dealing with arrays on my own, I understand them in theory but I need some help when it comes to getting practical. I asked my instructor to give me a couple of practices problems and he gave me the following.

using this as your main:

public static void main(String[] args) {

DatosPalabras datos = new DatosPalabras( "words.txt" );

JOptionPane.showMessageDialog(null, datos );

datos.sort();

JOptionPane.showMessageDialog(null, datos);

} 

(its in spanish so bear with me) create a class named DatosPalabras and words.txt and make sure your code can:

  • Read and display words.txt
  • Display the words in "words.txt" in alphabetical order

I really appreciate the help, I'm a bit stumped but I'm curious to know how I can accomplish this. Thank you!

EDIT:

    public class DatosPalabras {

public DatosPalabras(String string) {
    // read and display the content of words.txt


}

public void sort() {
    // need info on what to use in order to sort words instead of doubles and integers.

}

}
役に立ちましたか?

解決

In this example I have 1 file named Q19505617.java. Java only allows you to have 1 public class per file. It is the class that defines the main method. So this example works only because the DatosPalabras class is contained in that file. If you need DatosPalabras to be its own class then put the DatosPalabras in its own file named DatosPalabras.java and change the class signature to be public class DatosPalabras.

import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Q19505617 {
  public static void main(String[] args) {
    DatosPalabras datos = new DatosPalabras("words.txt");
    JOptionPane.showMessageDialog(null, datos);
    datos.sort();
    JOptionPane.showMessageDialog(null, datos);
  }
}

class DatosPalabras {
  private String[] lines;

  public DatosPalabras(String filename) {
    lines = new String[1];
    int lineCounter = 0;
    InputStream in = Q19505617.class.getResourceAsStream(filename);
    Scanner scanner = new Scanner(in);
    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      System.out.println(line);
      if(lineCounter == lines.length) {
        lines = Arrays.copyOf(lines, lines.length * 2);
      }
      lines[lineCounter] = line;
      lineCounter++;
    }
  }

  public void sort() {
    // put your real sort algorithm here. until then use this:
  }

  public String toString() {
    StringBuilder b = new StringBuilder();
    for (String line : lines) {
      b.append(line).append("\n");
    }
    return b.toString();
  }
}

他のヒント

You can create a reading Array like this:

 String[] Array = new String[number of lines in you txt file];
       int i = 0;

           // Selecting the txt file
           File theFile = new File("bla.txt");
           //Creating a scanner to read the file
           scan = new Scanner(theFile);
           //Reading all the words from the txt file
       while (scan.hasNextLine()) {
        line = scan.nextLine();
           Array[i] = line; // gets all the lines
           i++;

Then you create a method for sorting.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top