Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-15
5
Medium Priority
?
386 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 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

Industry Leaders: 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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