Results 1 to 2 of 2
  1. #1
    donohoes is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default checking if words in a txt file are in accending odder and displaying them

    hi guys i have a home work assenment due monday im for the life of me i cant figure out why this isnt working
    the program grabs words form a txt file then im trying to run them threw my check Word method to see if they are in ascending order
    and then return a true value if they are so i can print them in the compiler output
    but in stead of printing out the words this error comes out
    java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(Unknown Source)
    at DonohoestevenA2Q2.checkWord(DonohoestevenA2Q2.java :32)
    at DonohoestevenA2Q2.main(DonohoestevenA2Q2.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >
    any insight in to why the following code isnt working would be much appercated

    import java.io.*; // defines FileNotFoundException
    import java.util.Scanner;//duh
    public class DonohoestevenA2Q2 {

    //double duh
    public static void main(String[] args) throws FileNotFoundException{
    String theWord;
    boolean isAsce;
    /*int numWords;
    int length;
    int avLenght;
    int longLength;*/

    Scanner inputFile = new Scanner(new File("dictionary.txt"));
    while(inputFile.hasNextLine()){
    theWord = inputFile.nextLine();

    isAsce=checkWord(theWord);
    if(isAsce){
    System.out.println(theWord);
    }
    }
    }


    public static boolean checkWord(String word){
    char ch1;
    char ch2;
    boolean passFail=true;
    for(int i=0; i<word.length(); i++) {
    ch1= word.charAt(i);
    ch2= word.charAt(i+1);


    if(ch1>ch2){
    passFail=false;}
    }
    return passFail;
    }
    }

  2. #2
    donohoes is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: checking if words in a txt file are in accending odder and displaying them

    also if some one could tell me how to properly post code it would be nice
    update i fix the error by changing
    ch2= word.charAt(i+1); to ch2= word.charAt(i++);
    but now it just outputs every word regardless
    Last edited by donohoes; 10-19-2013 at 02:36 AM.

Similar Threads

  1. Number of words in file. Help.
    By itskeith in forum New To Java
    Replies: 1
    Last Post: 12-02-2012, 11:14 PM
  2. counting words within a file
    By justOne in forum New To Java
    Replies: 18
    Last Post: 07-25-2011, 02:15 PM
  3. Checking for forbidden words
    By überfuzz in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 12:08 PM
  4. Finding words (and more) in another file
    By Lund01 in forum Java Applets
    Replies: 5
    Last Post: 09-30-2010, 01:03 PM
  5. Checking if a file name already has .txt in it
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 12:45 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
  •