1

I have a "String line " variable in java when I give

System.out.println(line);

It gives me output as

233 81 335 332 165 56 82 316 123 247 328 348 177 89 86 1 252 228 34 350 116 91 193 106 272
121 56 203 179 259 25 163 101 77 116 135 29 257 254 400 195 192 20 59 270 350 116 92 6 87
22 230 126 207 393 73 399 57 96 93 9 196 172 111 192 234 315 59 35 351 390 239 88 339 145
17 161 330 328 198 47 194 107 231 312 260 118 369 344 114 355 99 117 199 219 5 255 336 124 

The above output is 4 separate lines.

Now I want to convert this string to int array, such that every location of the integer array will hold these numbers. I tried Tokenizing and parsing it, but when the data is very big nearly 200000 lines of record, it gives me a error.

Any Suggestions? Pls Help.

8
  • 1
    My suggestion is to try looking for something yourself. Google is a good start. Being spoon-fed answers is not going to help you get better. Commented Jun 19, 2014 at 4:43
  • it looks like this is your homework? Commented Jun 19, 2014 at 4:43
  • 1
    I'd suggest doing some research into String#split and Integer.parseInt Commented Jun 19, 2014 at 4:44
  • I tried Tokenizing it and then parsing it, but when this data is very big, nearly 200000 records, it gives error Commented Jun 19, 2014 at 4:46
  • 1
    Could be cause by an empty String I guess, you could use if (!value.trim().isEmpty()) { // Convert to int, where value is an indvidual element Commented Jun 19, 2014 at 4:57

4 Answers 4

4

Use Scanner Class Pass your Line (String) to Scanner Constructor and use methods hasNextInt() to check and nextInt() to fetch and store it to ArrayList<Integer> by the use of loop.

Sign up to request clarification or add additional context in comments.

1 Comment

You know, that's probably one of the better suggestions
1

Understanding this as a homework problem, instead of giving the answer, I will give some pointers. Look for the String.split(), Integer.parseInt() and for loop.

Comments

1

just to give you a hint, initialize an array int, try to loop to you string, split it by space or what separator you want then use parse or convert your string to int so you can add it on array of ints, then add that splited item to your array and print array to see that it is added.

basic loop, and array manipulation and you'll be good here.

Comments

1

You could pass that String into a scanner, and FOR every int that that the scanner reads, parse that token to an int using Integer.parseInt() and add it to a new array.

Scanner scan = new Scanner(yourString);
ArrayList<Integer> ints = new ArrayList<>();

while (scan.hasNext())
{
    ints.add(Integer.parseInt(scan.next()));
}

Be aware that will throw an exception if the token is NOT an int, so you will need to add some checking or surround it in a try-catch...

ints.toArray() will convert your ArrayList to an array, should you explicitely require that..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.