简体   繁体   中英

Would there be a cleaner/better way to write this

The code is suppose to read information from a file, create a object using that information, and then adding it to an ArrayList called servers.

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
        String line = "";
        while ((line = br.readLine()) != null) {
            String name = "";
            String ip = "";
            String port = "";
            String checkFrequency = "";
            int counter = 1;
            boolean alert = true;
            for (String value : line.split(",")){
                if (counter == 1){
                    name = value;
                }else if (counter == 2){
                    ip = value;
                }else if (counter == 3){
                    port = value;
                }else if (counter == 4){
                    checkFrequency = value;
                }else if (counter == 5){
                    alert = Boolean.parseBoolean(value);
                }
                counter++;
            }           
            MCServer server = new MCServer(name, ip, port, checkFrequency, alert);
            servers.add(server);
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Example line of what is stored in file:

Name,199.99.99.99,80,60,true

Would there be a better way to retrieve that information to be able to store it in the correct variable without using a loop with a counter the way shown above?

What about:

try (BufferedReader br = new BufferedReader(
       new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)))) {
    String line = null;  // start with null in case there is no line
    while ((line = br.readLine()) != null) {
        String[] tokens = line.split(",");
        MCServer server = 
            new MCServer(tokens[0], tokens[1], tokens[2], tokens[3], 
              Boolean.parseBoolean(tokens[4]));
        servers.add(server);
    }
}

You can just ignore the counter and use directly the tokens, but you should at least be sure there are enough tokens:

try
{
  BufferedReader br = new BufferedReader(new InputStreamReader());
  String line = null;
  String[] tokens;
  while ((line = br.readLine()) != null) {
     tokens = line.split(",");
     MCServer test = new MCServer(tokens[0],tokens[1],tokens[2],tokens[3],Boolean.valueOf(tokens[4]));
  }
}
catch (IndexOutOfBoundsException e) { // <- be sure to catch this
  // not enough elements in array
}

In addition you are passing strings as IP addresses and port, they are string but they should be checked against being convertible, so you could have Integer.valueOf(tokens[2]) for example just to raise a NumberFormatException in case.

Try this

 try {
    BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
    String line = "";
    while ((line = br.readLine()) != null) {
        String name = "";
        String ip = "";
        String port = "";
        String checkFrequency = "";
        int counter = 1;
        boolean alert = true;

        String s[] = line.split(",");
        name = s[0];
        ip = s[1];
        port = s[2];
        checkFrequency= s[3];
        alert = s[4];

        MCServer server = new MCServer(name, ip, port, checkFrequency, alert);
        servers.add(server);
    }
    br.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

I've revised your code a bit:

try {
        BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
        String line;
        while ((line = br.readLine()) != null) {

            String[] lineSplitted = line.split(",");

            String name = lineSplitted[0];
            String ip = lineSplitted[1];
            String port = lineSplitted[2];
            String checkFrequency = lineSplitted[3];
            boolean alert = Boolean.parseBoolean(lineSplitted[4]);

            MCServer server = new MCServer(name, ip, port, checkFrequency, alert);
            servers.add(server);
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Changes:

  • initialising the variables and then setting them is redundant, you never access them without setting them beforehand, so if you have your line splitted in the the first place you can initialise them with the values right away

  • for cycle is unnecessary if you're working with a single array each time, just access the correct elements of the array

  • catch blocks can be collapsed; IOException covers FileNotFoundException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM