-2

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.

}

}
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
  • Down voted because this question does not demonstrate any initial research by the questioner. What have you already tried. What are the expected verses actual results? – axiopisty Oct 21 '13 at 22:08
  • Sorry for being so vague, I created the class along with DatosPalabras in it. I also finished an excercise where I had to do something similar but comparing numbers ex. (ints and doubles) by listing them in ascending order using the sort methood. I just need to know what to do when it comes to characters instead of numbers. The results is to display the contents of the .txt file I'm creating in alphabetical order. Also, I appreciate the quick reply. – Gabriel O Figueroa Oct 21 '13 at 22:17
  • Are you saying that you need to create an array (rather than a collection) that contains text instead of numbers? Providing the DatosPalabras class as a code example would be helpful. – axiopisty Oct 21 '13 at 22:19
  • Yes, It needs to be an array, I will edit and add DatosPalabras in the OP. The .txt will be in my project so I just need to read it and sort it in alphabetical order. – Gabriel O Figueroa Oct 21 '13 at 22:22

2 Answers2

0

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.

Looptech
  • 213
  • 2
  • 4
  • 12
0

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();
  }
}
axiopisty
  • 4,972
  • 8
  • 44
  • 73
  • Thanks for the answer! But I still haven't used collections and part of what I'm trying to accomplish is to create the DatosPalabras class without modifying the given main class :( – Gabriel O Figueroa Oct 21 '13 at 22:56
  • All of this would work without modifying the main class in your code. I just wrote it like this to provide you with a SSCCE. I understand that you don't want to use Collections, but you still haven't asked any particular question regarding arrays. – axiopisty Oct 21 '13 at 22:59
  • Oh (noob moment) I thought that it was the main when I read the main line at the beginning. My apologies, my question is on the DatosPalabras methood, how can I get it to read and display the words on the .txt before having them sorted in the sort methoot. THe answer provided by Mccamer isn't working out for me since it give me and error in Array[i] = line; // gets all the lines – Gabriel O Figueroa Oct 21 '13 at 23:06
  • By the way, when exploring your code I get The nested type DatosPalabras cannot hide an enclosing type. I see you named the class twice, does that have something to do with this? Btw, I really appreciate the help you've given me thanks! – Gabriel O Figueroa Oct 21 '13 at 23:13
  • 1
    Perfect thanks, its amazing how you can accomplish something in different ways. I'd vote you up but my rep is circling the drain after too many noob questions. – Gabriel O Figueroa Oct 21 '13 at 23:31
  • You can always come back to upvote after your reputation increases. :) – axiopisty Oct 21 '13 at 23:36