0

I need a quick help with my code. I think it all works fine, but I have just one problem. I need to split every line of text file and add each of them to array. This assignment is about using RandomAccessFile class. And problem is - I can't split these lines from text file. It only says that each array is null.

I was typing any regex that I found online into String.split. Nothing works. I was also trying to use Scanner, but it went much worse.

try(RandomAccessFile raf = new RandomAccessFile("domy.txt", "rw")){
        while((str = raf.readLine()) != null){
            String[] data = new String[100];
            data = str.split("\n");
            String name = data[0];
            String floors = data[1];
            String price = data[2];
            String location = data[3];
            System.out.println(data[0] + " " + data[1] + " " + data[2] + " " + data[3]);

            if(floors.equals("1") && location.equals("Wies")){
                var floorsInt = Integer.parseInt(floors);
                var priceDouble = Double.parseDouble(price);
                raf.seek(raf.getFilePointer() - str.length());
                raf.writeBytes(name + "\n" + floorsInt + "\n" + df.format(priceDouble * 0.9) + "\n" + location);
            }
        }
    }
    catch(FileNotFoundException e){
        e.printStackTrace();

The most common errors are "Index 1 out of bounds for length 1" and "floors is empty"

Willa
3
1000000.00
Miasto
Chata
1
99999.99
Wies

Here is what I have in a text file

Janek
  • 1
  • 1
  • Please [edit] your question to provide a [mre] demonstrating the problem. – Slaw Jan 25 '23 at 21:41
  • @Slaw Here it is – Janek Jan 25 '23 at 21:44
  • 2
    If you do a readLine, isn't that reading until \n and your str won't contain any \n. Do yo want to split on a comma or some other character? – sleepyhead Jan 25 '23 at 21:49
  • @sleepyhead I want to split a text file that has one "string" on each line. I want the split to add first string to first array, second string to second array and so on. But I can't reach to Index 1. I need to find a split regex that could help me reach these further indexes. – Janek Jan 25 '23 at 22:01
  • None of your 'str' String contain '\n' (because you're reading into String line by line), so the array will be of size 1 - hence accessing [1] will result in an exception. Can you share an example file so we can understand better? – Mio Jan 25 '23 at 22:05
  • @Mio I just added it a second ago here, you can check it out – Janek Jan 25 '23 at 22:15
  • A string is a piece of text like "look mom, no hands!" With split you can pick a character on which which the string is split in an array of strings. E.g a space will yield 4 string in my example a comma will yield 2 and a line break (\n) will yield just the one string in data[0]. There will be no data[1]. The same in your example you do a readLine and one line at a time into str, which will be just to one line, there is nothing to split – sleepyhead Jan 25 '23 at 22:56
  • @sleepyhead Oh okay, I got it now. I got it wrong the whole time. But what's the correct way to add "3" from text file to data[1] and so on? – Janek Jan 25 '23 at 23:01
  • https://stackoverflow.com/questions/14098032/add-string-to-string-array If you make your data array into a ListArray you can data.add(str) in the while loop. Than close your loop and access the floor=data[2] – sleepyhead Jan 25 '23 at 23:13

0 Answers0