Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do i store the string into int[] arr?

Posted on 2011-03-15
5
Medium Priority
?
393 Views
Last Modified: 2012-05-11
i have some output in form:
some text: 1234 ms
some text: 345 ms
some text: 678 ms

i want to store these numbers into array.
my code:
while((s = br.readLine()) != null) {
                              if(s.contains("some text")){
                                    int i = Integer.parseInt(s.substring(34, 38));
                                    System.out.println("i="+i);
                                    
0
Comment
Question by:siddharthkool
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35144145
int[] array = new int[1000];
int index = 0;
while((s = br.readLine()) != null) {
                              if(s.contains("some text")){
                                    int i = Integer.parseInt(s.substring(34, 38));
                                    System.out.println("i="+i);
                                    array[index++] = i;
0
 

Author Comment

by:siddharthkool
ID: 35144194
i can print the 1234 as output but compiler throws error at next line 345.
Exception in thread "main" java.lang.NumberFormatException: For input string: "593 "
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 35144237
>                                     int i = Integer.parseInt(s.substring(34, 38));

you could change that to:


                                    int i = Integer.parseInt(s.substring(34, 38).trim());

though thats only going to work for 3 or 4 digit numbers.
any other size will fail
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35144412
Be sure the some text:(your mention) always 0 to 34  otherwise go for split method
0
 

Author Closing Comment

by:siddharthkool
ID: 35144461
problem solved.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question