0

What I am trying to accomplish is: User inputs three arrays of strings for example

1, 11, 111; 2, 22, 222, 2222; 3, 33, 333, 3333, 33333

I need to get rid of the , 's and put numbers into three arrays but it is storing weird results. Here's the code:

import java.util.Scanner;

public class signum {

    static int[] eja = new int[10000];
    static int[] tja = new int[10000];
    static int[] kja = new int[10000];

    private static String ej = null;
    private static String tj = null;
    private static String kj = null;

    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("Write a first array of integers");
        ej = sc.nextLine();

        ej = ej.replaceAll( "[^\\d]", " " );


        System.out.println("Write a second array of integers");
        tj = sc.nextLine();

        tj = tj.replaceAll( "[^\\d]", "" );



        System.out.println("Write the third array of integers");
        kj = sc.nextLine();

        kj = kj.replaceAll( "[^\\d]", " " );

        for(int i = 0; i < ej.length(); i++) {

            Character c = ej.charAt(i);

            if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' ||
                c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
                eja[i] = c;

                System.out.println(eja[i]);
            }
        }
    }
}

I know it still only tries to store the first array but the point is that if I try to store something like 1, 1, 1 it stored 49, 49, 49.

Also i still have no idea how to make it store numbers that are > 9, any ideas? Thanks in advance! I am really out of ideas here..

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
Artur
  • 140
  • 1
  • 10
  • 2
    Why don't you [split your strings](http://stackoverflow.com/a/3481842/264775) and then parse the individual elements using [Integer.parseInt](http://stackoverflow.com/a/5585800/264775)? – thegrinner Oct 01 '14 at 20:47
  • you can assign a char to an int in Java, but in a proper way: http://stackoverflow.com/a/17984993/1951298 – Curcuma_ Oct 01 '14 at 20:53

2 Answers2

2

You're reading in characters, and then (implicitly) converting them to integers when you store them in your int[]. But the problem is that a character of '1' doesn't end up as 1 when it gets converted to an int, it gets converted to its ASCII value, which is 49.

You want to read in a whole number, then use Integer.parseInt(String s) to convert it to an int.

Use String bits[] = ej.split(","); to get yourself an array of the String representations of the integers, and then use eja[i] = Integer.parseInt(bits[i].trim()) inside a for loop to get each int.

(You need the .trim() part to remove any extraneous spaces. If you're certain of the exact formatting of each line, you might use ej.split(", ") to split on a comma followed by a space, but that will mean it falls over if later on one turns up with extra spaces or no space.)

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • Now I see whats the deal here. Thanks alot! I have no clue why I didn't think of that! Thank you, once again! – Artur Oct 01 '14 at 20:53
  • It is still throwing exceptions at me when I am trying to store numbers to an array. Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1 1" I entered "1, 1, 1" – Artur Oct 01 '14 at 21:26
  • @Artur Take out your `replaceAll` lines! You don't want them any more. They're getting rid of the commas before you're splitting your `String`. – chiastic-security Oct 01 '14 at 21:28
  • Alright I did it and now I am getting ArrayOutOfBoundsException. But it prints out all the ej[i]'s just fine and then throws the exception. – Artur Oct 01 '14 at 21:33
  • 1
    @Artur Hard to work out what the error is without seeing the new code, but your `for` loop is probably not quite right. You should be using `for (int i=0; i – chiastic-security Oct 01 '14 at 21:42
-1

Your resulting arrays are of type Integer but you are putting Characters in there. This means that you will see the values differently. Redefine them as following and it will work:

static char[] eja = new char[10000];
static char[] tja = new char[10000];
static char[] kja = new char[10000];
Juru
  • 1,623
  • 17
  • 43