-1
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Process_TCPProbe_dm{

   public static void main(String[] args) throws IOException {
      
       //Extracting the port numbers from the file names passed in the arguments
       String portNumber1 = args[1].substring(9, 14);
       String portNumber2 = args[2].substring(9, 14);
       String portNumber3 = args[3].substring(9, 14);
              
       int i = 0;
       Scanner s = new Scanner(new FileReader(args[0]));
       //Initialising array of contents with length equals to number of lines in the input file
       String[] contents = new String[18];      
       while(true)
       {
           if (i == 18)
               break;//Skipping the last line of the file
           //Replaces the whitespace with comma and stores in the string array
           contents[i] = s.nextLine().replace(" ", ",");
          
           i++;  
       }
      
       BufferedWriter bw1 = new BufferedWriter(new FileWriter(args[1]));
       BufferedWriter bw2 = new BufferedWriter(new FileWriter(args[2]));
       BufferedWriter bw3 = new BufferedWriter(new FileWriter(args[3]));
       //Iterating the array of contents using a for each loop
       for(String each:contents)
       {
           //Writing the string to the corresponding file which matches with the portNumber
           //along with a new line
           if (each.contains(portNumber1))
           {
               bw1.write(each);
               bw1.newLine();
              
           }
           else if(each.contains(portNumber2))
           {
               bw2.write(each);
               bw2.newLine();
              
           }
           else if(each.contains(portNumber3))
           {
               bw3.write(each);
               bw3.newLine();
              
           }
       }
       //This is necessary to finally output the text from the buffer to the corresponding file
       bw1.flush();
       bw2.flush();
       bw3.flush();
      
       //Closing all the file reading and writing handles
       bw1.close();
       bw2.close();
       bw3.close();
       s.close();
   }

}

Here is my code, however, it only reads a file the size of 19 lines. I know i could use an ArrayList, but I'm not exactly sure how to go about it. I hope my question makes sense. If it'll help you understand better ill put the purpose of the code below.

This image is of the instructions for what the code is written for. It might just be extra information, but I put it here in case it'll better help to explain my question.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Danyelle
  • 7
  • 1
  • The questions you really ought to ask yourself are (a) why are you reading in line by line, and (b) why are you storing the lines in an array? If you just want to read the whole content of a text file to display as it is, it's better to read the whole thing at once (see the Files API for this). If you want to process line by line, you can do that without storing in an Array / ArrayList. If you are reading in parameters for a game, you should be initializing your variables instead of creating a String[] / ArrayList – ControlAltDel Jul 03 '20 at 22:54

2 Answers2

2

Just replace your array with Array Lins like this ArrayList<String> list = new List<>(); and then replace contents[i] = s.nextLine().replace(" ", ",");with list.add(s.nextLine().replace(" ", ","));. After this you will not need counter "i" anymore and break you loop in if condition. Just add scanner.hasNext() in a while condition.

2

Your looping condition should be based on Scanner.hasNextLine and you don't need the variable i. Use an ArrayList to score an unknown amount of elements.

Scanner s = new Scanner(new FileReader(args[0]));
final List<String> contents = new ArrayList<>();  
while(s.hasNextLine()){
   contents.add(s.nextLine().replace(" ", ","));
}

This can be shortened using Files.lines. Note that this throws an IOException that you will need to handle.

final List<String> contents = Files.lines(Paths.get(args[0]))
    .map(s->s.replace(" ", ",")).collect(Collectors.toList());
Unmitigated
  • 76,500
  • 11
  • 62
  • 80