Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree2Likes

Thread: trouble with FILE I/O

  1. #1
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default trouble with FILE I/O

    Hello everyone,

    I'm trying to finish this program that reads two files in, one that is a poem and one that has words to be censored out of that poem. I've got plenty of code that seems like it is doing nothing at the moment.

    I cannot get the words censored!! I've posted my code below, it's most of the code, all that it needs to work except some text files.

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    
      //---------------------------------------------
    public class CensorApp
    {
      private String cmdLine, currentLine = "", textFile, openFile;
      private String cmd;
      private Vector<String> output, censor;
      //--------------------------------------------- 
      public CensorApp() throws FileNotFoundException
      {
        output = new Vector<String>();
        censor = new Vector<String>();
        
       Scanner input = new Scanner(System.in);
       while(input.hasNextLine())
       {
         cmd = input.nextLine();
         Scanner cmdScan = new Scanner(cmd);
         if(cmdScan.hasNext())
         {
           String inCmd = new String(cmdScan.next());
       if(inCmd.equalsIgnoreCase("h"))
         help();
       else if(inCmd.equalsIgnoreCase("l"))
         load();
       else if(inCmd.equalsIgnoreCase("o"))
         open();
       else if(inCmd.equalsIgnoreCase("c"))
         censor();
       else if(inCmd.equalsIgnoreCase("s"))
         show();
       else
         System.out.println("Command not recognized, type h for help.");
       }
    }
      }
                     
      //---------------------------------------------
      public void load1()throws FileNotFoundException
      {
        output.removeAllElements();
    Scanner scanner = new Scanner(new File("baa.txt"));
    while(!currentLine.equals("q") && scanner.hasNextLine())
      output.add(scanner.nextLine());
    print();
    scanner.close();
      }
     //--------------------------------------------- 
      public void load() throws FileNotFoundException
      {
           
        Scanner input = new Scanner(System.in);
          while(input.hasNextLine())
          {
            cmdLine = input.nextLine();
            Scanner inLine = new Scanner(cmdLine);
            if(inLine.hasNext())
            {
             cmd = inLine.next();
             if(cmd.equalsIgnoreCase("l"))
               while(inLine.hasNext())
               {
                 textFile = inLine.next();
                 if(textFile.equalsIgnoreCase("baa.txt"))
                     load1();
               }     
            }  
          }
      }
    
    //----------------------------------------------
      public void open() throws FileNotFoundException
      {
            Scanner input = new Scanner(System.in);
          while(input.hasNextLine())
          {
            cmdLine = input.nextLine();
            Scanner inLine = new Scanner(cmdLine);
            if(inLine.hasNext())
            {
             cmd = inLine.next();
             if(cmd.equalsIgnoreCase("o"))
               while(inLine.hasNext());
             {
               openFile = inLine.next();
             Scanner opeFile = new Scanner(new File("test" + openFile));
             }
            }
          }
      }
    //---------------------------------------------
      public void censor1()throws FileNotFoundException
      {
        censor.removeAllElements();
    Scanner scanner = new Scanner(new File("baa-delete.txt"));
    while(!currentLine.equals("q") && scanner.hasNextLine())
      censor.add(scanner.nextLine());
    
    scanner.close();
      }
     //---------------------------------------------
      public String censor() throws FileNotFoundException
      {
            Scanner input = new Scanner(System.in);
          while(input.hasNextLine())
          {
            cmdLine = input.nextLine();
            Scanner inLine = new Scanner(cmdLine);
            if(inLine.hasNext())
            {
             cmd = inLine.next();
             if(cmd.equalsIgnoreCase("c"))
               while(inLine.hasNext())
               {
                 textFile = inLine.next();
                 if(textFile.equalsIgnoreCase("baa-delete.txt"))
                     censor1();
                }     
            }  
             inLine.close();
    }
       String c1 = censor.toString();
       String o1 = output.toString();
       Scanner sc1 = new Scanner(c1);
       Scanner so1 = new Scanner(o1);
        while(sc1.hasNext())
              {
                String delWord = sc1.next();
                while(so1.hasNext())
                {
                  String word = so1.next();
                  if(word == delWord)
                  {
                    int x = word.length();
                    char aster[] = new char[x];
                     for(int i = 0; i<x; i++)
                    {
                    aster[i] = '*';
                 
                    }
                     String outputs = new String(aster);
                     return o1;
                  }
                }
              }
           sc1.close();
           so1.close();
           return null;
        
      }
    
    
    //-----------------------------------------------
      public void show() throws FileNotFoundException
      {
          String currentLine = "";
          Scanner input = new Scanner(System.in);
          while(input.hasNextLine())
          {
            cmdLine = input.nextLine();
            Scanner inLine = new Scanner(cmdLine);
            if(inLine.hasNext())
            {
              cmd = inLine.next();
            while(inLine.hasNext());
             {
               openFile = inLine.next();
             Scanner scanner = new Scanner(new File("test" + openFile));
          currentLine = scanner.nextLine();
          System.out.print(currentLine);
             }
    
            }
          }
      }
     //---------------------------------------------      
           public void print( )throws FileNotFoundException
       {
         
          PrintWriter p = new PrintWriter( 
          new File("test"+textFile));
               
             p.println(output);
             p.close();   
           }  
      //-----------------------------------------------
      public void help()
      {
    
       
         System.out.println("Load filename: Saves the corresponding file under a separate name to be edited.");
         System.out.println("Open filename: Opens the editing version of the filename.");
         System.out.println("Censor filename: Censors select words with *** marks and saves under the editing file name.");
         System.out.println("Show filename: Prints the specified file to the command screen.");
         System.out.println("Help: Prints this list of commands and descriptions.");
         System.out.println("Quit: Closes the system input scanner.");
       
      
      }
     //--------------------------------------------- 
           
      public static void main(String[] args) throws FileNotFoundException
      {
        CensorApp app = new CensorApp();
      }
    }
    Last edited by Norm; 12-01-2011 at 11:12 PM. Reason: added code tags

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: trouble with FILE I/O

    Here's the deal: posting elebenty gazillion lines of unformatted code and saying "it doesn't work" is going to get you zero replies. When posting code place [ code ] before and [ /code ] after (without the spaces) to preserve formatting. Post full error messages. Ask a specific question. It would help us if you could debug your code as much as possible to track down where the problem occurs. That way we don't have to do it. After all it is your code, the onus is upon you to fix it not us.
    Dark likes this.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    If your file I/O code works, comment it out for testing the censor method.
    Manually load the Vectors with some data and just execute the censor method.
    Your console I/O is not needed for the testing of the censor method and is not something I want to take time to do.
    Make the test code so that it executes without any user intervention and shows the problem.

  4. #4
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    Sorry about the code up top.

    Here is where the issue is, I'm trying to replace a word in a vector (output) with a string of asterisks of the same length IF the word in the vector output matches one of the words in the vector censor.

    Here is the method I'm using:

    Java Code:
      public void censor(File textFile) 
      {
       String aster = "";
       String c1 = censor.toString();
       String o1 = output.toString();
       Scanner sc1 = new Scanner(c1);
       Scanner so1 = new Scanner(o1);
        while(sc1.hasNext())
           {
             String delWord = sc1.next();
             while(so1.hasNext())
             {
               String word = so1.next();
               int z = word.length();
               int x = output.indexOf(word);
               for(int i = 0; i<z; i++)
               {
                 aster += "*";
               } 
               System.out.print(aster);
               output.set(x, aster);
             }
             
             }  
          }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    Can you make a simple program that compiles and executes to show your problem?
    Have the program completely self-contained and not requiring the reading of files.
    You can use a Scanner constructor with Strings for input vs a file:
    Scanner scnr = new Scanner("Line 1\nLine 2\n...<more lines as needed here>");

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: trouble with FILE I/O

    Alarm bells should ring whenever a parameter is never used in a method.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: trouble with FILE I/O

    More alarm bells ring when a method/class/variable have the same name.

  8. #8
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    Alright I cut down the code to just show exactly what's happening and where I need some assistance.

    I take a word from the censor vector and I try and find that word in the output vector, if there is a match I need to get the length of the word and create a string of asterisks the same length of the word. Once that loop finishes I replace the word in the output vector and that's where the thing crashes with an out of bounds exception.

    java.lang.ArrayIndexOutOfBoundsException: -1 (line 46)


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class CensorApp
    {
    
      private String cmdLine;
      private String cmd;
      private Vector<String> output, censor;
     
      public CensorApp() throws FileNotFoundException
      {
    
        output = new Vector<String>();
        censor = new Vector<String>();
        Scanner input = new Scanner(System.in);
    
        censor.add("baa");
        censor.add("any");
        censor.add("three");
          System.out.println(censor.toString());
          
        output.add("baa baa black sheep, \n");
        output.add("have you any wool, \n");
        output.add("yes sir, yes sir, three bags full.");
           System.out.println(output.toString());
          
       String aster = "";
       String c1 = censor.toString();
       String o1 = output.toString();
       Scanner sc1 = new Scanner(c1);
       Scanner so1 = new Scanner(o1);
        while(sc1.hasNext())
           {
             String delWord = sc1.next();
             while(so1.hasNext())
             {
               String word = so1.next();
               int z = word.length();
               int x = output.indexOf(word);
               for(int i = 0; i<z; i++)
               {
                 aster += "*";
               } 
               System.out.print(aster);
               output.set(x, aster);
             }
             
             }  
             System.out.println(output.toString());
          }
        
     public static void main(String[] args) throws FileNotFoundException
      {
        CensorApp app = new CensorApp();
      }
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    What is the value of x when statement 46 is executed? Add a println to show it.
    How did it get that value?

  10. #10
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    Hmmm, I just saw it's x that's out of bounds. I don't know how it got that way, but I'll start the process right now.

    Thanks for the heads up Norm.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    Add more printlns to show the values of the variables you are working with.
    Add id Strings to the printlns so you can easily see where it was printed:
    Java Code:
        System.out.println("censor=" + censor.toString());

  12. #12
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    I went through it with all the printlns real quick, but it's showing me that output.indexOf method for any of the words in the vector returns -1.

    I'm pretty sure I'm using the indexOf method syntactically correct so I don't see why it would return -1 for a word that's in the vector.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    Did you print out the values of ALL the variables you were using, not just x?
    You need to SEE the values that the program is working with.
    Dark likes this.

  14. #14
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    I posted printlns for all variables at the time they are called and each previous one every time a new variable is called.

    It returned


    Censor: [baa, any, three]
    Output: [baa baa black sheep,
    , have you any wool,
    , yes sir, yes sir, three bags full.]
    Aster :
    cl: [baa, any, three]
    o1 :[baa baa black sheep,
    , have you any wool,
    , yes sir, yes sir, three bags full.]
    Aster
    z: 4
    cl :[baa, any, three]
    o1 :[baa baa black sheep,
    , have you any wool,
    , yes sir, yes sir, three bags full.]
    word :[baa
    delWord :[baa,
    Aster: ****
    x: -1
    z: 4
    cl: [baa, any, three]
    o1 : [baa baa black sheep,
    , have you any wool,
    , yes sir, yes sir, three bags full.]
    word :[baa
    delWord: [baa,


    Would the delWord be screwing up because of the comma? If not, than it's definitely the indexOf method for output. It starts out as -1. All the other variables are working correctly so far as I can tell.
    Last edited by dru36; 12-02-2011 at 09:33 PM.

  15. #15
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    wait, the bracket is part of the baa and it's returning as part of the length. So there would be no index of that word because it's not part of the vector... But how do I remove it?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    Would the delWord be screwing up
    I don't see where delWord is used in the code you posted. You can comment it out and not change what your code does.
    What about the value of word? Does that look correct?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    You are using the formatted String returned by the Vector class's toString() method.
    You could use String's substring method to strip off the []s

  18. #18
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: trouble with FILE I/O

    I trimmed up the words, but the x is still returning as -1. Here's what I've changed of my code up above.

    Java Code:
       while(sc1.hasNext())
           {
          
             String delWord = sc1.next();
             while(so1.hasNext())
             {
               String word = so1.next();
               int z = word.length();
            int windex = word.indexOf("[");
              int dindex = delWord.indexOf("[");
               word = word.substring(windex+1);
             int d2index = delWord.indexOf(",");
             delWord = delWord.substring(dindex+1, d2index);
    System.out.println(d2index);
    System.out.println(dindex);
    System.out.println(delWord);
    System.out.println(word);
           if(word.equals(delWord))
           {
      
               int x = output.indexOf(word);
    System.out.println(x);
               for(int i = 0; i<z; i++)
               {
                 aster += "*";
               } 
           
               output.set(x, aster);
           }
                 
             }
             
             }  
             System.out.println(output.toString());
          }

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: trouble with FILE I/O

    What does your debug print out show?

  20. #20
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Log file trouble
    By kovalensue in forum Advanced Java
    Replies: 3
    Last Post: 04-03-2011, 06:13 PM
  2. Having trouble opening and reading a txt file
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 01-29-2011, 03:55 AM
  3. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 02:11 AM
  4. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-22-2010, 12:29 AM
  5. Replies: 0
    Last Post: 12-01-2008, 10:50 PM

Tags for this Thread

Posting Permissions

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