0

I can't get my scanner to read in the integer from a text file.

The file looks something like this (a matrix size followed by a matrix)

3
0 1 1 
1 1 1 
1 0 0  

These two methods belong to ReadMatrix

   public void openFile() {
      try {
         scanner = new Scanner(System.in); // create a scanner object
         System.out.println("File to read: ");

         fileName = scanner.nextLine(); // get name of file
         System.out.println("File " + fileName + " is opened.");
         br = new BufferedReader(new FileReader(fileName));
      } catch (Exception e) {
         System.out.println("can't find the funky file");
      }
   }

   public int readInteger() {
      sc = new Scanner(System.in);
      // while(sc.hasNextInt()){
      // sc.nextLine();
      anInt = sc.nextInt();
      System.out.println("Trying to read integerrrrrr help");
      // }
      return anInt;
}

Here are 2 classes


public class MainMethodHere {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      RunMainMethod running = new RunMainMethod(); 
   }
}


public class RunMainMethod {
   ReadMatrix a = new ReadMatrix(); 
  public  RunMainMethod(){
     a.openFile();
     a.createFile();
     System.out.println("just createdFile"); //<--- this is where the program ends
     a.readInteger();  
     System.out.println("just readInteger");
     a.helpMeWrite();
     System.out.println("Just write sample ");

     a.closeResources();
  }
}

Writing to the file works fine, but I can't get the program to read the given text file to start the manipulation.

I tried finding the solutions in a few SO posts: how to read Integer from a text file in Java Reading numbers from a .txt file into a 2d array and print it on console How to read integer values from text file

I also tried ReadFile() and WriteFile(), but when I try to track my code via printing into the console, my simple integers shows up as different values. Is that something related to bytes? So I try reverting back to using a scanner, but somehow it's not working. Please let me know if I should clarify something further. I am new to coding and SO.

0 Answers0