Results 1 to 11 of 11
  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default java longest word in array

    I have a text file that I scan in and store the words into an array, I want to create a method now that organizes the list of words from longest to shortest.

    Could anyone help me?

    public void openFile(){

    try{

    Scanner sc = new Scanner(new File("puzzle.dat"));
    while(sc.hasNext()){
    if(words.length == size()){

    String answer = sc.next() + "\n";
    String descrip = sc.nextLine() + "\n";
    PuzzleWord puzzle = new PuzzleWord(answer, descrip);
    words[count] = puzzle;
    //System.out.println(words[count]);
    puzzle.toString();
    count++;
    }
    }

    sc.close();
    }
    catch(IOException a){
    System.out.println(a);
    }
    catch(NoSuchElementException a){

    }

    }

    public PuzzleWord[] wordsSort(PuzzleWord[] word){
    PuzzleWord[] tempwords = new PuzzleWord[words.length];

    return tempwords;
    }

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    a for loop that goes through the array and edits a String longestWord using an if statement could work.
    e.g
    Java Code:
    if (words[i].length() > longestWord.length())
    longestWord = words[i];
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    i keep getting the cannot find symbol - variable length()

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    he's just providing you with example code. make the extra effort to apply it to the code you have, please.

  5. #5
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    ummm thats what i've been trying to do for the last hour

  6. #6
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    i keep getting the cannot find symbol - variable length()
    Does not help us out. Post all of your relevant code (in your case it's probably everything) and the output the compiler gives you.

    Mr. Beans

    Note: Make sure you use the code tags as well. When you click on the "Post Reply" button and it brings you to a reply page click the "#" button and insert the code between the tags.

  7. #7
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    I have the file loading to an array and printing into a textArea. Now i just need to sort the words in the file into a new array that has the longest word first in line, then the second longest, etc.



    import java.io.*;
    import java.util.*;

    public class PuzzleFile
    {
    private int count = 0;
    private PuzzleWord[] words = new PuzzleWord[20];
    private Scanner sc = new Scanner(System.in);
    PuzzleWord[] tempwords = new PuzzleWord[words.length];

    public PuzzleFile(){
    this.openFile();
    }

    public void openFile(){

    try{

    Scanner sc = new Scanner(new File("puzzle.dat"));
    while(sc.hasNext()){
    if(words.length == size()){

    /**String answer = sc.next() + "\n";
    String descrip = sc.nextLine() + "\n";
    PuzzleWord puzzle = new PuzzleWord(answer, descrip);
    words[count] = puzzle;
    //System.out.println(words[count]);
    puzzle.toString();
    count++;*/

    }
    else{

    String answer = sc.next() + "\n";
    String descrip = sc.nextLine() + "\n";
    PuzzleWord puzzle = new PuzzleWord(answer, descrip);
    words[count] = puzzle;
    //System.out.println(words[count]);
    puzzle.toString();
    count++;
    }
    }

    sc.close();
    }
    catch(IOException a){
    System.out.println(a);
    }
    catch(NoSuchElementException a){

    }

    }

    public PuzzleWord[] wordsSort(PuzzleWord[] word){
    // PuzzleWord[] tempwords = new PuzzleWord[words.length];
    for(int i = 0; i < tempwords.length;i++){
    if(word.length > tempwords.length){
    tempwords[i] = word[i];
    tempwords = word;

    }
    }

    return tempwords;

    }

    public int size(){
    return count;
    }

    public void expandCapacity(){
    PuzzleWord[] bigger = (PuzzleWord[]) (new PuzzleWord[words.length * 2]);
    for(int i = 0; i < count; i++){
    bigger[i] = words[i];
    words = bigger;
    }
    }

    public String list(){
    String name = "";
    //String line = "";
    for(int i = 0; i < count; i++){
    name = name + words[i].getWord();
    }
    return "Answers: " + "\n" + "\n" + name.toString() + "\n";
    }

    public String descript(){
    String des = "";
    for(int i = 0; i < count; i++){
    des = des + words[i].getDes();

    }

    return "Descriptions:" + "\n" + "\n" + des.toString() + "\n";
    }

    public String toString(){
    String result = "";
    for(int i = 0; i < count; i++){
    result = result + words[i].toString();
    System.out.println(result);
    }

    return result;
    }
    }

  8. #8
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Well since you still didn't use the code tags I'm not really going to bother trying to read through your code. I hate reading through unformatted code.

    However, there is a method for doing what you want to do. It's called an insertion sort.

    Simple treat all the words like numbers using the length() method for making your program working with the insertion sort.

    If you look hard enough, wikipedia links directly to sites which give you live examples and explanations of insertion sorts in java.

    Mr. Beans

  9. #9
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    I'd create class Word implements Comparable<Word> to compare by the length of each word and just use Arrays.sort

    1. read the file into an array
    2. sort the array
    3. write the array back to file

    Too easy.

  10. #10
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    This is what I am trying to do to get the longest word in the array to print first but it is not working, can anybody help me?


    public String list(){
    String name = "";
    String longest = "";
    //String line = "";
    for(int i = 0; i < count; i++){
    name = name + words[i].getWord();
    if(name.length() > longest.length()){
    longest = name;
    }
    }
    return "Answers: " + "\n" + "\n" + longest.toString() + "\n";
    }

  11. #11
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    name = name + words[i].getWord();
    if(name.length() > longest.length()){
    longest = name;

    here is an issue. name keeps getting longer and will be longer than what it was before (unless the word is "").

Similar Threads

  1. Word
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 04:25 AM
  2. [SOLVED] Help on Word and Character counting in java
    By Alistair in forum New To Java
    Replies: 2
    Last Post: 05-15-2008, 04:48 AM
  3. Replies: 2
    Last Post: 04-28-2008, 11:56 AM
  4. Replies: 13
    Last Post: 01-09-2008, 07:51 AM
  5. How to update Existing Word Doc Using java
    By umashankar in forum Advanced Java
    Replies: 1
    Last Post: 07-27-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •