Solved

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

Posted on 2011-03-15
5
363 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
Comment Utility
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
Comment Utility
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
Comment Utility
>                                     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
Comment Utility
Be sure the some text:(your mention) always 0 to 34  otherwise go for split method
0
 

Author Closing Comment

by:siddharthkool
Comment Utility
problem solved.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now