简体   繁体   中英

Array of strings to be put in to Vector

I have this problem in the below code as it does not run but generated nullpointer exception. What i am trying to do here is get an input an array of strings then spit it but comma and then pass it to Integer type and then store it in a vector. Then get the maximum number from that array. Code shows no errors but hard to find whats wrong.

import java.util.Collections;
import java.util.Vector;

public class Splitting {

  /**
  * @param   
  */

protected int[] temp;
Vector<Integer> vec = new Vector<Integer>();

public void split(String input) {
    if (input == null) {
      String[] str;
      str = input.split(",");
      temp = new int[str.length];

      for (int i = 0; i < str.length; i++) {
            temp[i] = Integer.parseInt(str[i]);
            vec.add(temp[i]);
        }
    }
    System.out.println(vec);
    Collections.sort(vec);

    System.out.println(vec);
    Collections.max(vec);
}



public static void main(String[] args) {
    // TODO Auto-generated method stub

    Splitting obj = new Splitting();

    obj.split("12,65,21,23,89,67,12");



}

}

You have to create an array. Replace

temp = null;

with

temp = new int[str.length];

Another slight problem: you test input != null in a while loop after you've used it for the split. So if your method is called with a null value, you'll get a NPE even before the while statement is executed. You can remove the while statement and add a test

if (input == null) {
   // return or throw exception
}

at the beginning of your method.


You're close with your code - I think I can show a reference implementation to compare:

public void split(String input) {
  if (input == null) {
     // if the input is null, print a message and return
     System.out.println("Input must not be null");
     return;
  }

  String[] numbers = input.split(",");  // changed the name to show the content
  int[] temp = new int[numbers.length];

  for (int i=0; i < numbers.length; i++) {
     try {
        temp[i] = Integer.parseInt(str[i]);
     } catch (NumberFormatException nfe) {
        // if a number can't be parsed, print a message and return
        System.out.println("Input contains an illegal entry (non-integer value");
        return;
     }
     vec.add(temp[i]);
  }
  System.out.println(vec);
  Collections.sort(vec);

  System.out.println(vec);
  Collections.max(vec);
}

Your problem lies in this line temp = null; . Remove this line. This is throwing your NPE. You are trying to access a field that is null.

Instead declare temp inside the split method.

int [] temp = new int [str.length];

CoolBeans and AndreasD have pointed out that temp should not be initialized to null. Let me add, temp shouldn't exist as an array, period. You are creating a duplicate structure, once as the array temp and the other time as the Vector vec.

So, here are some possible changes. temp just becomes an int (not array) and everywhere you have temp[i] , replace with temp . Or, get rid of temp entirely and just have vec.add(Integer.parseInt(str[i])); .

You could do like this

String[] strArr = "12,65,21,23,89,67,12".split(",");
int[] intArr = new int[strArr.length];

for (int i = 0; i < strArr.length; i++) {
    intArr[i] = Integer.parseInt(strArr[i]);
}

Ints.max(intArr); // 89, with Google Guava Ints.max

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