Solved

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

Posted on 2011-03-15
5
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

739 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