Hello,
I'm trying to read from the command line.
There are 9 numbers and I want to store them in an array as int.
I've alread the following

public static void main(String[] args)  throws IOException{
		
String [] temp;

		public static void main(String[] args) throws IOException {

		

		InputStreamReader inputStream = new InputStreamReader(System.in);
		BufferedReader keyboard = new BufferedReader(inputStream);

		System.out.println("Enter eight numbers from 0 to 9");

		String initialstate = keyboard.readLine();
		String delimiter = ",";
		temp = initialstate.split(delimiter);
		for (int i = 0; i < temp.length; i++)

My questions are:
how can I convert the string into a array and store it as an array.

int[] intArray = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
    try {
        intArray[i] = Integer.parseInt(temp[i]);
    } catch(NumberFormatException e) {
        System.out.println("Invalid int encountered:- " + temp[i]);
}

This will transfer your String objects from temp String array to a new array of int primitives.

Hello,
I'm trying to read from the command line.
There are 9 numbers and I want to store them in an array as int.
I've alread the following

public static void main(String[] args)  throws IOException{
		
String [] temp;

		public static void main(String[] args) throws IOException {

		

		InputStreamReader inputStream = new InputStreamReader(System.in);
		BufferedReader keyboard = new BufferedReader(inputStream);

		System.out.println("Enter eight numbers from 0 to 9");

		String initialstate = keyboard.readLine();
		String delimiter = ",";
		temp = initialstate.split(delimiter);
		for (int i = 0; i < temp.length; i++)

My questions are:
how can I convert the string into a array and store it as an array.

And may I add that you need to declare the temp array in the main. Also, I don't know if it is a problem with copying your code, but you have 2 main methods.

your code should be:

public static void main(String[] args) throws IOException {

        String[] temp;


        InputStreamReader inputStream = new InputStreamReader(System.in);
        BufferedReader keyboard = new BufferedReader(inputStream);

        System.out.println("Enter eight numbers from 0 to 9");

        String initialstate = keyboard.readLine();
        String delimiter = ",";
        temp = initialstate.split(delimiter);
        int[] intArray = new int[temp.length];

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

            for (int x = 0; x < temp.length; x++) {
                try {
                    intArray[x] = Integer.parseInt(temp[x]);
                } catch(NumberFormatException e) {
                    System.out.println("Invalid int encountered:- " + temp[x]);
               }
            }
        }

        System.out.println("Numbers array contents: ");
        for (int num: intArray) {
            System.out.println("Number: [" + num + "]");
        }

    }

your code should be:

public static void main(String[] args) throws IOException {

        String[] temp;


        InputStreamReader inputStream = new InputStreamReader(System.in);
        BufferedReader keyboard = new BufferedReader(inputStream);

        System.out.println("Enter eight numbers from 0 to 9");

        String initialstate = keyboard.readLine();
        String delimiter = ",";
        temp = initialstate.split(delimiter);
        int[] intArray = new int[temp.length];

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

            for (int x = 0; x < temp.length; x++) {
                try {
                    intArray[x] = Integer.parseInt(temp[x]);
                } catch(NumberFormatException e) {
                    System.out.println("Invalid int encountered:- " + temp[x]);
               }
            }
        }

        System.out.println("Numbers array contents: ");
        for (int num: intArray) {
            System.out.println("Number: [" + num + "]");
        }

    }

thanks for that.
there is still a problem:
if I input the following data:
012345678 then the 0 is not stored in the array.
how can I also store the 0 in the array?
how can I use this array in another method?
so I want to use the int array in another method.

your code in your first post:

String delimiter = ",";
temp = initialstate.split(delimiter);

is giving instructions to split int values from a String which ints are separated using a ","

Your number: 012345678 is being treated as a whole int value and Mathematically does not make any sense that a number begins with a "0"

If you pass for example: "0,12345678" or "12345678,0" 0 will be stored in your int array in both cases !

oh, thank you

Also if you want that array to be used in another method. Just declare a method that takes as argument an int array and pass it as parameter.

yes, that would do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.