Solved

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

Posted on 2011-03-15
5
372 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 50 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 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