Skip to content
Advertisement

String.valueOf(Integer) always return 1

I’m trying to parse String “2 2 2” and add every symbol ‘2’ in ArrayList. This is my code:

charCounter = 0;
rawSize = 0;
while(charCounter < line.length()){
    a_char = line.charAt(charCounter);
       if(a_char != ' '){
           System.out.print(String.valueOf(a_char)+" ");
           temp = Character.digit(a_char, 10);
           myIntArray.add(temp);
           System.out.println(String.valueOf(myIntArray.get(rawSize)));
           rawSize++;
    }
    charCounter++;
}

I can’t understand why System.out.println(String.valueOf(myIntArray.get(rawSize)));always return 1 ?

UPDDATE:

I try read file, which contain next text:

1 1 1
2 2 2
3 3 3

Here is my Main function:

public static void main(String[] args){

char a_char = ' ';
int charCounter = 0;
int temp = 0;
String line = null; 
String file = "C:\Users\KUser\Desktop\SImpleIteration\matrix.txt";
BufferedReader reader = null;

ArrayList myIntArray = new ArrayList();

    try {
        reader = new BufferedReader(new FileReader(file));
    } catch(FileNotFoundException fnfe) { 
        System.out.println(fnfe.getMessage());
    }

    try {
        while ((line = reader.readLine()) != null) {
            charCounter = 0;
            rawSize = 0;
            while(charCounter < line.length()){
                a_char = line.charAt(charCounter);
                if(a_char != ' '){
                    System.out.print(String.valueOf(a_char)+" ");
                    temp = Character.digit(a_char, 10);
                    myIntArray.add(temp);
                    System.out.println(String.valueOf(myIntArray.get(rawSize)));
                    rawSize++;
                }
                charCounter++;
            }
        }
    } catch(IOException ioe) {
        System.out.println(ioe.getMessage());
    }
};

My output:

1 1
1 1
1 1
2 1
2 1
2 1
3 1
3 1
3 1

When i replace:a_char = line.charAt(charCounter); with a_char = '5';, my output:

5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5

Can’t understand why so.

Advertisement

Answer

I have just rewritten your code to be able to compile it, and it works correctly:

String line = "2 2 2";
ArrayList myIntArray = new ArrayList();
int charCounter = 0;
int rawSize = 0;
while(charCounter < line.length()){
    char a_char = line.charAt(charCounter);
    if(a_char != ' '){
        System.out.print(String.valueOf(a_char)+" ");
        int temp = Character.digit(a_char, 10);
        myIntArray.add(temp);
        System.out.println(String.valueOf(myIntArray.get(rawSize)));
        rawSize++;
    }
    charCounter++;
}

Output:

2 2
2 2
2 2

Update: I have edited my code according your update. And again, it works correctly, but String.valueOf(...) indeed always return 1. This is because in the outer while you assign rawSize to zero, so you will always print the first three elements of myIntArray, which are ‘1’s. Try to print the whole myIntArray, and you will see that it’s filled as you expected.

P.S. There are really a lot of more concise and nice ways to accomplish this, as another responder suggests, for instance.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement