1

I have a String array

String[] list = new String[]{"2","A","B","1","C","3","D","E","F"}

Please Note that the first element of array is 2. I would have done -

int n = Integer.parseInt(list[0]);

Problem is, I need a loop to go through the array. I cant seem to parse it without giving me a NumberFormatException error. Also, notice that if the number is a 2, it will arrange the next 2 elements (might be random) and then a space.

1: A

2: B

3: -empty-

4: D

5: E

6: F

7: -empty-

8: C

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
Aurora_Titanium
  • 472
  • 1
  • 8
  • 23
  • 1
    Can't you `try` it and then do something else with it? – ChiefTwoPencils Sep 17 '15 at 05:15
  • 1
    possible duplicate of [What's the best way to check to see if a String represents an integer in Java?](http://stackoverflow.com/questions/237159/whats-the-best-way-to-check-to-see-if-a-string-represents-an-integer-in-java) – ChiefTwoPencils Sep 17 '15 at 05:16
  • Blindly you can't do it. Why would you even do it ? As your problem statement says, converting whole to array to integer doesn't seem your purpose. – SacJn Sep 17 '15 at 05:17
  • Do you mean to say that the number in the array specifies the number of elements that are string following in the array and retrieve the string elements only? – Blip Sep 17 '15 at 05:26
  • @aurora_titanium please post a reply to my previous comment as a new comment if you require help – Blip Sep 17 '15 at 05:43
  • You don't have to face NFE if you first read digits then read digits many characters without converting them to integer. Control is in your hands so you can definitely avoid NFE – SacJn Sep 17 '15 at 05:57

4 Answers4

0

Iterate through the array and check whether the selected character is a digit or not. If it's a digit use parseInt.

Here's a tutorial on how to identify digits. What is the best way to tell if a character is a letter or number in Java without using regexes?

Good luck.

Community
  • 1
  • 1
Nimila Hiranya
  • 4,842
  • 10
  • 35
  • 52
0

Use a try/catch statement in your loop when you iterate over the array.

Here's a short example:

for(int i = 0; i < list.length; i++) {
    try {
        int n = Integer.parseInt(list[i]);
        // do whatever you need to do with n here
    }
    catch (NumberFormatException nfex) {
        // do something else with the current element if need be
    }
}
Hoppeduppeanut
  • 1,109
  • 6
  • 20
  • 29
0
for(String hexNbr : list) 
{
   System.out.println("Next number is " + Integer.parseInt(hexNbr, 16);
}

Your input is hex, so you have to use parseInt(String, radix).

Brad
  • 2,261
  • 3
  • 22
  • 32
0

Algorithm is simple enough.

  • Parse number
  • Shift N values 1 position to the left
  • Assign empty value to vacant space
  • Advance past block, and repeat

Progress is a follows:

Start: "2","A","B","1","C","3","D","E","F"
N = 2
Shift: "A","B","B","1","C","3","D","E","F"
Empty: "A","B","-","1","C","3","D","E","F"
N = 1
Shift: "A","B","-","C","C","3","D","E","F"
Empty: "A","B","-","C","-","3","D","E","F"
N = 3
Shift: "A","B","-","C","-","D","E","F","F"
Empty: "A","B","-","C","-","D","E","F","-"

Code

String[] list = {"2","A","B","1","C","3","D","E","F"};
for (int i = 0; i < list.length; ) {
    int len = Integer.parseInt(list[i]);
    System.arraycopy(list, i + 1, list, i, len);
    list[i + len] = "-empty-";
    i += len + 1;
}
for (String value : list)
    System.out.println(value);

Output

A
B
-empty-
C
-empty-
D
E
F
-empty-
Andreas
  • 154,647
  • 11
  • 152
  • 247