I am having trouble taking a String type of data, extracting information from it, and then putting that information into an array. I have to extract the integers from the string while ignoring all non-digit characters
Ex: String: "223aiwij233.02 93"
Output: Array {223, 233, 02, 93}
The problem lies in the intParse method. My problem is that when I pull out the integers, the program is 'making up' numbers. Right now, the code breaks if there is more than 1 consecutive non-digit in the string and will run, but incorrectly if the String has only 1 non-digit character between integers. I have posted most of the code. After extracting the numbers, I will have to sort them, but I have already figured that part out.
public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println("Enter a string: "); String s = stdIn.next(); //String s from user int[] parsed = new int[10]; //Array parsed extracted from string s int[] sorted = new int[10]; //Array sorted from array parsed //call intParse method parsed = intParse(s); //call intSort method; sorted = intSort(parsed); stdIn.close(); } public static int[] intParse(String s) { int[] numArray = new int[10]; //creating array to insert elements from string int j = 0; //counter String number = ""; //empty string to hold integers from String s for (int i=0; i<s.length(); ++i) { if (Character.isDigit(s.charAt(i))) { number += s.charAt(i); //if the current character is a digit, add it to the number string. if not a digit, run code under else statement. } else { System.out.println(number); //print the current number to screen numArray[j] = numToArr(number); //take the current string number and add it to next available array index by running the numToArray method ++j; //increase j so the next empty array index will be used number = ""; //set the number string back to a blank and continue through for loop } } return numArray; } public static int numToArr(String b) { int realNumber = Integer.parseInt(b); return realNumber; }