Search code examples
javaindexinglocationeof

How can I reach End Of File?


I am designing a program that saves the index location for specific characters from a message. Then, I need to retrieve these characters according to their index location. I kept the locations for these characters in a .txt file. I retrieved them, but at the end, I got this message "Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 120 out of bounds for length 120". My codes:

int n;
String s; 
int lineNumCount = 0;
String coverText = stegoMsg.getText(); // get the stego text from the textfield
int k = coverText.length(); // get the length for the stego text
int lineNumb = 1;
Scanner myFile = null;
          try{
               Scanner file = new Scanner(new File("location.txt"));//location.txt is the file that has the locations for the characters
               myFile = file;
             }catch (FileNotFoundException e){
                 System.out.println("File does not found");
             }
        while (myFile.hasNextLine()){
            //Count the Number of the lines in location.txt
            //1. Read the File
            File fileLocation = new File("location.txt"); 
            if(fileLocation.exists()){
                try {
                    FileReader fr = new FileReader(fileLocation);
                    LineNumberReader lr = new LineNumberReader(fr); //2. Read the lines for location.txt
                    
                    while((lr.readLine()) !=null){
                    lineNumCount++;
                            }
                   // System.out.println("Total Number of the Lines " + lineNumCount);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            
           for(int x = 0; x<lineNumCount; ++x){
            try{
                String line = Files.readAllLines(Paths.get("location.txt")).get(lineNumb);
               // System.out.println("Line First " + line);
                BufferedReader bufrd = new BufferedReader(new FileReader("SFile.txt")); //SFile.txt is the file that has the messsage that I need to take the location for the specific characters
                int nn = Integer.parseInt(line);  
                s = bufrd.readLine();
                System.out.println("The Location " + nn  + " is : "+ s.charAt(nn)); // read the character that has this location
                lineNumb++;
                
                }catch(IOException e){
                    System.out.println("Line 334");
                }
           }
            }
        }
        myFile.close();
 }

Is it possible to guide me on how I can solve the exception? I appreciate any help you can provide.


Solution

  • Here is the solution ...

            int n;
            String s; 
            int lineNumCount = 0;
            String coverText = stegoMsg.getText(); // get the stego text from the textfield
            int k = coverText.length(); // get the length for the stego text
            int lineNumb = 1;
            Scanner myFile = null;
            try{
            Scanner file = new Scanner(new File("location.txt"));
            myFile = file;
            }catch (FileNotFoundException e){
                System.out.println("File does not found");
            }
           try{
            while (myFile.hasNext()){
                //Count the Number of the lines in location.txt
                //1. Read the File
                File fileLocation = new File("C:\\Users\\Farah\\Dropbox\\Steganography codes\\NewStegoTech\\location.txt");
                if(fileLocation.exists()){
                    try {
                        FileReader fr = new FileReader(fileLocation);
                        LineNumberReader lr = new LineNumberReader(fr); //2. Read the lines for location.txt
                        
                        while((lr.readLine()) !=null){
                        lineNumCount++;
                                }
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
              try{
                    String line = Files.readAllLines(Paths.get("location.txt")).get(lineNumb);
                   // System.out.println("Line First " + line);
                    BufferedReader bufrd = new BufferedReader(new FileReader("Stego File.txt"));
                    int nn = Integer.parseInt(line);  
                    s = bufrd.readLine();
                    System.out.println("The Loocation " + nn  + " is : "+ s.charAt(nn)); // read the character that has this location
                    lineNumb++;
                    }catch(IOException e){
                        System.out.println(e);
                    }
               
                }
            }
           }catch(IndexOutOfBoundsException e)
           {
               System.out.println("Finish reading file");
           }
           
            myFile.close();