0

have some problem reading a file in java and save each element into 2 arrays. my txt is made like this

2,3
5
4
2
3
1

where the first line is the lenght of two array A=2 and B=3 and then the element of each array. I don't know how to save them into A and B and initialized the array with their lenght. At the end each array will be A=[5,4] B=[2,3,1]

public static void main(String args[])
      {
        try{
// Open the file that is the first 
// command line parameter

            FileInputStream fstream = new FileInputStream("prova.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
//Read File Line By Line
            while ((strLine = br.readLine()) != " ")   {
                String[] delims = strLine.split(",");
                String m = delims[0];
                String n = delims[1];
                System.out.println("First word: "+m);
                System.out.println("First word: "+n);
            }
//Close the input stream
            in.close();
            }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
                }
        }
    }

this is what i made..i used System.out.println.... just to print in console it's not necessary...Someone can help me, give me some advice? thanks in advance

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
user1841492
  • 161
  • 2
  • 2
  • 11
  • [Don't compare strings with `==` or `!=`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) instead use `equals()` method. – Pshemo Aug 18 '13 at 00:26
  • @Pshemo: yep, I saw that after I posted this. He shouldn't even be checking for that, and shouldn't have a while loop at all in his code, but rather for loops since he will know how many times he is supposed to loop before the loops. – Hovercraft Full Of Eels Aug 18 '13 at 00:28
  • 1
    To the original poster: the key to solving most programming problems is to break the big problem down into little steps, and then try to solve each little step one at a time. Again, your code should not contain any while loops at all but rather for loops since you'll know how many times you want to loop *before* each loop. You'll also want to close your BufferedReader in a finally block. – Hovercraft Full Of Eels Aug 18 '13 at 00:29

2 Answers2

1

Again, break the big problem into little steps, solve each step.

  1. Read first line.
  2. Parse first line to get sizes of the 2 arrays.
  3. Create the arrays.
  4. Loop first array length times and fill the first array.
  5. Loop second array length times and fill second array.
  6. Close BufferedReader in a finally block (make sure to declare it before the try block).
  7. Show results.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Match this answer with the steps outlined in @Hovercraft's answer

String strLine = br.readLine(); // step 1

if (strLine != null) {
  String[] delims = strLine.split(","); // step 2

  // step 3
  int[] a = new int[Integer.parseInt(delims[0])];
  int[] b = new int[Integer.parseInt(delims[1])];

  // step 4
  for (int i=0; i < a.length; i++)
    a[i] = Integer.parseInt(br.readLine());

  // step 5
  for (int i=0; i < b.length; i++)
    b[i] = Integer.parseInt(br.readLine());

  br.close(); // step 6

  // step 7
  System.out.println(Arrays.toString(a));
  System.out.println(Arrays.toString(b));
}

Notice, I called br.close(). With in.close() you're closing the inner stream only and that leaves BufferedReader still open. But closing the outer wrapper stream closes all the wrapped inner streams automatically. Note, that clean-up code like this should always go in a finally block.

Also, there's no need to have DataInputStream and InputStreamReader in the chain. Just wrap BufferedReader around a FileReader directly.

If all these classes are having you a bit confused; just remember Stream classes are used for reading at the byte level and Reader classes are used to read at character level. So, you only need Readers here.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • 1
    Impressive code. But wouldn't it have been better to let the OP discover his own impressive code? You're taking the fun, excitement and learning out of coding by handing him a spoon-fed answer. – Hovercraft Full Of Eels Aug 18 '13 at 00:42
  • Sure down-vote my http://stackoverflow.com/questions/11335203/calendar-add-method-error-when-going-beyond-the-year question if you want, rep doesn't matter to me, or better yet, address my concerns in a comment here as to why you decided to spoon-feed the OP and what you hope he can gain by it. – Hovercraft Full Of Eels Aug 18 '13 at 00:54
  • Do check the +5 I just gave you. (+4/-1) – Ravi K Thapliyal Aug 18 '13 at 00:57
  • Thank you, but again, please address my concerns so I can remove my down-vote on this answer. Isn't it better to give suggestions to the original poster, but to let them create the code for themselves? We haven't seen his reply to my answer yet to see what he can do first for himself. – Hovercraft Full Of Eels Aug 18 '13 at 00:58