This discussion is archived
3 Replies Latest reply: Sep 28, 2011 2:32 PM by 796440 RSS

Calculate the number of rows and items in the file

890788 Newbie
Currently Being Moderated
Hello, people

Next problem.

I have method lenghtArray(string fileName)

When I call it, the program just hangs. Set breakpoints, writes:

FileNotFoundException(Throwable).<init>(String) line: 195 and etc.

The method I use to a different method (createMatrix(String fileName)) to create an array and pass all the values ​​from the file.

Please, help. Why not work?

Its my code listing:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


public class Matrix 
{
     public static double[][] createMatrix(String fileName) 
     {
         double[][] myArray; 
               
             //длина массива
             int[] tempArr = lenghtArray(fileName);  
             int i = tempArr[0];
             int j = tempArr[1];
             int n = 0;
             int k = 0;
          
             //Создаю массив
            myArray = new double[i][j];
            try
             {
                 FileReader fil = new FileReader(fileName);
                  Scanner data = new Scanner(fil);
                   
           
              while(data.hasNextLine())
              {
                    //ArrayList line = new ArrayList();
                   Scanner lineData = new Scanner(data.nextLine());
               
                   while(lineData.hasNext())
                   {
                       //Добавляем значения в массив
                        myArray[n][k] =  Double.parseDouble(lineData.next());
                    
                   }
               
                   n++;
                   k = 0;//обнуляем 
              }
          }
          catch(Exception e)
          {
               System.out.println("File not found!"); 
               
          }
                        
          return myArray;
          
     }//createMatrix
          
     
     public static int[]lenghtArray(String fileName)
     {
          int i = -1;
          int j = -1;
          int jTotal = 0;
         try
         {
              FileReader fil = new FileReader(fileName);
              Scanner sc = new Scanner(fil);
     
               while(sc.hasNextLine())
             {
                   Scanner lineData = new Scanner(sc.nextLine());
                  i++;
                   while(lineData.hasNext())
                   {
                        j++;
                        if(j > jTotal)
                         jTotal = j;
                   }
                   j = -1;
             }
     }
     catch(Exception e)
     { System.out.println("File not found!"); }
          int[] lenghtArr = new int[]{i, jTotal};
          return lenghtArr;
          
     }
}



ublic class MyClass {

     public static void main(String[] args) 
     {
          String path = "src/in.txt";
          double[][] ar = Matrix.createMatrix(path);
          System.out.println("arr[i][j]");
          
          
          show(ar);
          
          
          
          
          
          
     }
     
     private static void show(double[][] arr)
     {
          for(int i = 0; i < arr.length; i++)
               for(int j = 0; j < arr[0].length; j++)
                   System.out.println(arr[i][j]);
          
     }

}
  • 1. Re: Calculate the number of rows and items in the file
    DrClap Expert
    Currently Being Moderated
    First you call Scanner.hasNext() to see if the Scanner has anything for you to process. Your next step should be to call Scanner.next() to get that thing, and then to process it.

    But since you never call next(), the Scanner always has something waiting for you. You made this error in both of the loops which use Scanners.
  • 2. Re: Calculate the number of rows and items in the file
    890788 Newbie
    Currently Being Moderated
    Can you for example show, whe need i call method next() and why?
  • 3. Re: Calculate the number of rows and items in the file
    796440 Guru
    Currently Being Moderated
    887785 wrote:
    Can you for example show, whe need i call method next() and why?
    He already told you both of those things.

    When:
    First you call Scanner.hasNext() to see if the Scanner has anything for you to process. Your next step should be to call Scanner.next()
    Why:
    to get that thing
    Please do spend some time thinking about the responses you get, as well as writing code to follow the advice given. Even if you don't understand the answer 100%, it should be enough to give you an idea of what to try, and you should be able to play around with it for a little bit. If you still are stuck after that, post again, but remember, the burden is on you to do the hard work.

    Edited by: jverd on Sep 28, 2011 2:30 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points