Hi, I have a problem and forgive me if this sounds like a stupid question but I'm not really a good programmer. Ok, this is my problem. Say you have a string and you would like to pull out integers from the string what do you do?
Example:
String question = "I have 20 different textbooks in my bag but you have 4" or
String order = "Time take :11, jumps: 12,3,456,43,23,56"
How do you input the jumps into a 2d array. PLEASE HELP

Recommended Answers

All 6 Replies

would depend.
do you know beforehand how the string object will look like? and with this, I mean: do you know before running the application at which position there will be (an) integer(s)?
otherwise, it might be quite tricky, you'll have to check char by char whether it's a digit, and whether it's part of a larger number or stands on itself.

why would you use a 2d array? I just see data for a single dimension array.
you just use the capabilities of the Integer class to turn every number into an int and store it in the array as you would with every other integer.

the thing it the data is extracted from a file, so you can't really tell the position so you cant use charAt() and I need to place it into a two dimensional array because its used to connect the distance between two position x and y.

ehm... yes, you can use charAt() (there are other ways too, but ...)

String original = "yep this 123 is a number 456 so was that";
String number = "";        
String numbers = "0123456789";
for ( int i = 0; i < original.length(); i++ ){
      if ( numbers.contains(""+original.charAt(i))){
          while ( numbers.contains(""+original.charAt(i))){
              number += original.charAt(i);
              i += 1;
         }
         System.out.println("number = " + number);
         number = "";
      }
}

this can do part of what you need.. now, you implement this in your code and show how you convert it to an int and store it in an array.

Thanks so much :), will give it a go. Thank you

Sorry but I ran the tested code through a file to find the numbers but it would usually print out a load of blanks and then numbers but stop and give this error.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at TSM.<init>(TSM.java:32)
at TSM.main(TSM.java:15)
thing is it doesn't read all the numbers like its got a limit or something.

which is why I said:

this can do part of what you need

the error you describe is what happens if the last character of your inputted String object is a number.

so, if you just want to keep the code 'as is', just add a character of which you know isn't a number at the end of the 'original' String before going through the loop

or, what I would recommend ... investigate it yourself a bit and see how you can improve the code yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.