Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

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

  1. #1
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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!


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default 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.

  3. The Following User Says Thank You to PhHein For This Useful Post:

    ashl7 (April 10th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

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

    Quote Originally Posted by PhHein View Post
    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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default 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.

  7. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

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

    Quote Originally Posted by ashl7 View Post
    .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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  8. #7
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default 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).

  9. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  10. #9
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by Norm View Post
    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

  11. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  12. #11
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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

  13. #12
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default 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()

  14. The Following User Says Thank You to PhHein For This Useful Post:

    ashl7 (April 11th, 2013)

  15. #13
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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:
     
    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?

  16. #14
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default 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.

  17. The Following User Says Thank You to PhHein For This Useful Post:

    ashl7 (April 11th, 2013)

  18. #15
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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

  19. #16
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

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


  20. #17
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  21. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    ashl7 (April 11th, 2013)

  22. #18
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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

  23. #19
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

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

    Teach your self programming in ten years ?

  24. The Following User Says Thank You to PhHein For This Useful Post:

    ashl7 (April 12th, 2013)

Similar Threads

  1. "Reduce" Method and Adding Rational Numbers
    By LoganC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2012, 03:04 PM
  2. "Separate Numbers"-Problem:Assigning Variables to User Inputs.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2012, 06:58 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. The program working fine but the "NO" button doesn't work
    By ahcenpg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2011, 07:32 PM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM