Working with numbers when there's "space" in between them

Hi, I'm working on a program about matrices...for getting the different elements of the matrix I must prompt the user as below:

prompt example: Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: user input example: 1 2 1 2 13 1 4 50 3

now I need to put each number of user input in an element of a 3*3 matrix...if the user input was like 12121314503, I could have used % and / to get each number and assign it to an element of array...or use "parse" methods to change from string to int..... but how can I do such things when space is in between the numbers?!

I really have no clue! any help would be much appreciated!

Re: Working with numbers when there's "space" in between them

You can either user String.split() to get a String array containing the individual numbers as Strings, or you can use String.replaceAll() to get rid of the spaces. I'd use the split version.

Re: Working with numbers when there's "space" in between them

Are you trying to get single decimal digits from the user's input no matter how they were entered?

1 23 and 12 3 and 123 and 1 2 3 would all be considered the same input: 1,2,3

Re: Working with numbers when there's "space" in between them

Quote:

Originally Posted by

**PhHein**
You can either user String.split() to get a String array containing the individual numbers as Strings, or you can use String.replaceAll() to get rid of the spaces. I'd use the split version.

This is exactly how you should do it, but i would put my vote in for the replaceAll version. replaceAll would get rid of all the spaces then you could reference each character (or number in this case) by using charAt.

Re: Working with numbers when there's "space" in between them

Hmm, in the OP he had a 13 and a 50, so that would rule out the charAt() solution.

Re: Working with numbers when there's "space" in between them

Quote:

Originally Posted by

**ashl7**
.if the user input was like 12121314503, I could have used % and / to get each number and assign it to an element of array

This line implies that digits are independent otherwise i would agree with you. I was assuming his example showed how some numbers would have spaces and some wouldn't.

Re: Working with numbers when there's "space" in between them

12121314503 is 11 chars so there must be two 2 digit numbers or one 3 digit number to fit into his 3x3 matrix. Or he has to omit remaining digits. Whatever, without him specifying the exact problems we're only guessing (my crystal ball is currently in service).

Re: Working with numbers when there's "space" in between them

Mindless speculation with minimal facts and no input from the real subject matter expert. We better stop before we turn into CNN, Fox News, or Sportcenter.

Re: Working with numbers when there's "space" in between them

Quote:

Originally Posted by

**Norm**
Are you trying to get single decimal digits from the user's input no matter how they were entered?

1 23 and 12 3 and 123 and 1 2 3 would all be considered the same input: 1,2,3

sorry for the delay...and thanks for the responses

all the digits after a space has to be considered one number, for example 1 23 4 7 568 should be considered as 1,23,4,7,568....so the element of a[0][0]=1, a[0][1]=23 and so on.

hope I was clear enough :-B

Re: Working with numbers when there's "space" in between them

Sounds like the split should work for you. Then just output it into a 3x3 array. Are you still having problems?

Re: Working with numbers when there's "space" in between them

I think I'm good (for now lol)....I was wondering how to use the method String.split()....did some google and found out it has to be like: StringName.split(" "), with space in between quotation mark!!!

it's a pretty cool method

thank you guys :)

Re: Working with numbers when there's "space" in between them

One hint don't google, but go straight to the API docs: String.split()

Re: Working with numbers when there's "space" in between them

looks like I'm actually having a problem

this is the code I wrote so far in order to get a matrix:

Code :

package matrix.inverse;
import java.util.*;
public class MatrixInverse {
public static void main(String[] args) {
System.out.println("Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ");
String strNumbers = new String();
Scanner x = new Scanner(System.in);
strNumbers = x.nextLine();
String[] arrStrNumbers = new String[8];
arrStrNumbers = strNumbers.split(" ");
int[] numbers = new int[8];
for(int i = 0; i<=arrStrNumbers.length ; i++) //converting array of string to array of integers
{
numbers[i] = Integer.parseInt(arrStrNumbers[i]);
}
//making the matrix
int[][] matrix = new int[2][2];
numbers[0]=matrix[0][0];
numbers[1]=matrix[0][1];
numbers[2]=matrix[0][2];
numbers[3]=matrix[1][0];
numbers[4]=matrix[1][1];
numbers[5]=matrix[1][2];
numbers[6]=matrix[2][0];
numbers[7]=matrix[2][1];
numbers[8]=matrix[2][2];
System.out.println(matrix[2][0]);
}
}

I'm trying to test it in the last line( System.out.println(matrix[2][0]); )...but it gives me this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8

at matrix.inverse.MatrixInverse.main(MatrixInverse.ja va:28)

Java Result: 1

what do u think is the problem?

Re: Working with numbers when there's "space" in between them

int[] numbers = new int[8]; this is an array of length 8, i.e. the index range is [0,7]. You're trying to access index 8.

Re: Working with numbers when there's "space" in between them

GODDAMN IT!!!!!

wait but I changed it to 9, still get the same error, except it's 9 instead of 8 in the error: java.lang.ArrayIndexOutOfBoundsException: 8

Re: Working with numbers when there's "space" in between them

Re: Working with numbers when there's "space" in between them

Your issue is here: for(int i = 0; i<=arrStrNumbers.length ; i++)

array.length returns the actual length of the array. The length is always 1 more than the last index which can cause a lot of problems. You are using "i<=length" you should be using "i<length" because the actual value of length is out of the array index bounds. Make sense?

Re: Working with numbers when there's "space" in between them

changed it, still gives me error!!!!

but found it finally

same thing u said about arrays(confusing length and indexes)...I made a mistake in declaring the matrix array...it should have been a [3][3] instead of [2][2]!!

aaah, I have a looooooong way to go to be a programmer!!lol

Re: Working with numbers when there's "space" in between them