Java Unione di due serie di array da un input dell'utente - Come faccio a trattare con i null?

StackOverflow https://stackoverflow.com/questions/4801658

  •  22-10-2019
  •  | 
  •  

Domanda

Sto scrivendo un programma per l'assegnazione di classe CS.

In sostanza è un metodo che serve per la linea di comando. Qualcosa di simile a un file CSV, in modo da chiamare la chiamata che faccio merge seguito essere il csv di.

ad esempio di unione 1,2,3,4 5,6,7,8

Questo farà due cose. 1) richiede ogni lista come parametro di matrice si fonde poi diventare 1 matrice grande, 2) ordina tale matrice.

Ecco la cattura, dalla riga di comando che dobbiamo trattare con valori nulli. Quindi, un utente potrebbe alimentare in:

merge 1,2,, 3,4 5,6

Come faccio a trattare con questo?

esempio di output di errore:

Enter commands:
merge 12,,2 43
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:493)
    at java.lang.Integer.parseInt(Integer.java:514)
    at CmdInterpreter.strArrayToIntArray(CmdInterpreter.java:143)
    at CmdInterpreter.getIntArray(CmdInterpreter.java:130)
    at Assign1.processOneCommand(Assign1.java:99)
    at CmdInterpreter.processCommands(CmdInterpreter.java:198)
    at CmdInterpreter.processCommands(CmdInterpreter.java:230)
    at CmdInterpreter.ooMain(CmdInterpreter.java:243)
    at MyAssign1.main(MyAssign1.java:20)
È stato utile?

Soluzione 3

I found out you can feed in either list1 as null or list2 or both. But not elements of those arrays... Meaning I was trying something that would not be a problem for this assignment.

Altri suggerimenti

When you call parseInt, wrap the call in a try/catch block. If you catch a NumberFormatException, throw away the string and decrement the number of values.

Edit: How do you do that? Something like this:

int[] extractValues(String arg) {
    String[] sValues = arg.split(",");
    int n = values.length;
    int[] values = new int[n];
    for (int i = 0; i < n; ++i) {
        try {
            values[i] = Integer.parseInt(sValues[i]);
        } catch (NumberFormatException e) {
            --n;
        }
    }
    // reallocate values array if there were problems
    if (n < values.length) {
        int[] tmp = new int[n];
        System.arraycopy(values, 0, tmp, 0, n);
        values = tmp;
    }
    return values;
}

You need to handle the event that there is no integer there. It appears that you are simply blindly looping through the input without checking for it's integrity, which is why you are getting an exception.

The proper way to do this would be to set up a checking mechanism which ensures that the current data you are parsing is valid (in this case, meaning it is an Integer).

I am leaving my answer very vague because you explicitly stated that this is for a school assignment, so you should take this answer and use it to experiment and learn to implement a proper solution.

StringUtils.split should give you about ten ways to do this.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top