0

so I have this method which reads a file into an arrayList of strings and I want to swap 2 characters from 2 strings in the list, update the list and save it back to the file. I seem to have everything but the list update part working. When I try the .set() method I get an index out of bounds error and can't quite understand why

public class CharSwapper {
public static void linear(String name, int firstl,int first, int secondl,int second){
    try{

    List<String> lines = Files.readAllLines(Paths.get(name));

    String one = Files.readAllLines(Paths.get(name)).get(firstl);
    String two = Files.readAllLines(Paths.get(name)).get(secondl);

    char sone[]=one.toCharArray();
    char stwo[]=two.toCharArray();
    char temp;

    temp=sone[first];
    sone[first]=stwo[second];
    stwo[second]=temp;


    String ssone=new String(sone);
    String sstwo=new String(stwo);

    //one=ssone;
    //two=sstwo;

    lines.set(firstl,ssone);
    lines.set(secondl, sstwo);

    FileWriter writer = new FileWriter(name); 
    for(String str: lines) {
      writer.write(str+'\n');
    }
    System.out.println("Edit succesfull");
    writer.close();
igrilkul
  • 45
  • 8
  • 2
    list is empty perhaps? Reading the file doesn't work? – Lucas Aug 02 '16 at 15:53
  • 3
    Calling `Files.readAllLines(Paths.get(name))` over and over is very inefficient. Just use it once, and then call `lines.get(firstl);` and `lines.get(secondl);` – 4castle Aug 02 '16 at 15:54
  • @Lucas the list is not empty and it works, it reads the lines into string objects -4castle will do, thanks – igrilkul Aug 02 '16 at 15:56
  • @PeterLawrey no this is a different problem that I'm having. – igrilkul Aug 02 '16 at 15:57
  • 2
    Always post the complete stack trace of the exception you're asking about. It contains critical information. – JB Nizet Aug 02 '16 at 15:58
  • @PeterLawrey The error though is not being caused by the algorithm, its by the file. – 4castle Aug 02 '16 at 16:00
  • @igrilkul I wouldn't read the file three times, just once, then there is no chance the number of lines will be different. – Peter Lawrey Aug 02 '16 at 16:00
  • 1
    @JBNizet Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at src.CharSwapper.linear(CharSwapper.java:15) at src.Tobi.main(Tobi.java:49) – igrilkul Aug 02 '16 at 16:00
  • 1
    I think there's a bit of confusion. You're talking about swapping **2 characters**, but you also appear to be trying to swap 2 **lines**. Until we're clear on what you want to do, we can't really help you. Please give us also a sample input and a sample output so we can be sure of what you try to achieve. For now, it seems like you try to load a file containing N lines, and to swap char at position `first` in line at position `first l` with char at position `second` in line at position `secondl`. Is that correct? – haylem Aug 02 '16 at 16:00
  • 2
    It should be in the question, not in a comment. And if you read it, you'll notice that the exception happens when calling `list.get()`, at line 15, not `list.set()`. So the list just doesn't contain one of the indices passed to the method. – JB Nizet Aug 02 '16 at 16:01
  • Apparently I'm an idiot and the file had been wiped, sorry for the trouble guys, I've learned my lesson. – igrilkul Aug 02 '16 at 16:05
  • 1
    Also, beware of the readAllLines API: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllLines-java.nio.file.Path- . Specifically: `whether the List is modifiable or not is implementation dependent and therefore not specified`. So you should rather recreate a new list. – haylem Aug 02 '16 at 16:05

0 Answers0