Two Laptop Bag
The moose likes Beginning Java and the fly likes how to extract two numbers from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "how to extract two numbers from a string" Watch "how to extract two numbers from a string" New topic
Author

how to extract two numbers from a string

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
this is my code.


this is my output

Please enter your 3 numbers (e.g. 123)
234
userNumber234
userInput234
firstNumbers2
lastNumbers3
firstNumberOfInput2
secondNumbersOfInput3


randomNumber802
stringRandomNumber802
firstNumbersOfRandomString8
lastNumbersOfRandomString0
firstNumbersOfRandom8
secondNumbersOfRandom0


how can i make the strings "First Numbers" and "Last Numbers" retain the value of of the 0,1 1,2 values of the input and random?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

how can i make the strings "First Numbers" and "Last Numbers" retain the value of of the 0,1 1,2 values of the input and random?


substring (0,1) will only fetch you the first character. And substring (1,2) will only fetch you the second character. Is this what you want?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
no i want this:
789
first = 78
last = 89
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

nathan gibson wrote:no i want this:
789
first = 78
last = 89


The substring indexes are inclusive for the start, and exclusive for the end -- so you want substring(0,2) and substring(1,3).

Henry
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
thank you very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to extract two numbers from a string
 
Similar Threads
Need help with my Lottery Program
99 bottles of beer song.......
operator cannot be applied to ints.
Using percent Modulus to calculate total number of even numbers from user input
flipping coin program