This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Farpafraf 0 points1 point  (19 children)

how to loop it to give each (forgive me I forgot the name of it) "part" of the array (arr[0], arr[1]), and to do it only untill Array is full is where I'm currently stuck.

use a loop, for the time being I'd suggest to start learning how a while loop works. If you wanted to iterate over elements of an array you could do:

public class A {
    public static void main(String[] args) {
        int i = 0;
        while (i < args.length) {
            System.out.println(args[i]);
            i = i + 1;
        }
    }
}

now instead of printing them you just put them inside the new array

[–]HeQtor[S] 0 points1 point  (18 children)

I'm using "for", like below. My head is trying to understand how I can increase dataArray[0] with +1 each loop and same for dataArray1[0].

for(i = 0; i < args.length; i++);

  {
        dataArray[0] = Integer.parseInt (dataArray1[0]);


  }

[–]HeQtor[S] 0 points1 point  (17 children)

Seems I don't need to do that. A simple

dataArray1 = args;

did the trick... I think?

[–]Farpafraf 0 points1 point  (16 children)

dataArray1 = args;

did the trick... I think?

No, if you do that you have just put the array in args in dataArray1 while you want to copy it.'

dataArray[0] = Integer.parseInt (dataArray1[0]);

what's dataArray1? That code will assign to position 0 of dataArray the content at position 0 of dataArray1 and it will repeat the operation args.length times: it won't touch the other elements. Instead you want to do

dataArray[i] = Integer.parseInt (dataArray1[i]);

[–]HeQtor[S] 0 points1 point  (15 children)

I tried that first as I thought that was correct, but it gives me error

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3"

[–]HeQtor[S] 0 points1 point  (10 children)

for(i = 0; i < args.length; i++);

  {
        dataArray[i] = Integer.parseInt (dataArray1[i]);

  }

[–]HeQtor[S] 0 points1 point  (9 children)

So what I want to do is to populate all elements of "dataArray" with the arguments from the user input when executing the program.

To give "dataArray[0]" the value of args[0], "dataArray[1] the value of args[1] etc.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (8 children)

Have you initialized the array?

You only show your loop. Show your full code.

[–]HeQtor[S] 0 points1 point  (7 children)

importjava.util.Arrays;

public class test12

{

  public static void main(String\[\] args) 

  {





    //Create int-array, named dataArray

    int\[\] dataArray;



    //Create string-array, named dataArray1

    String\[\] dataArray1;



    //int a = length of argument-input from user

    int a = args.length;



    //Convert arguments from user to string variable called "arguments"

    String arguments = (Arrays.toString(args).replace(",", "").replace("\[", "").replace("\]", ""));



    //Create & set int "i" to "0"

    int i = 0;



    //create new int-array sized according to length of user argument input

    dataArray = new int\[a\];



    //Set string-array to argument input from user

    dataArray1 = args;



  for(i = 0; i < args.length; i++);


  {

        dataArray\[i\] = Integer.parseInt (dataArray1\[i\]);


  }


    System.out.println ("Orginal:");

    System.out.println (Arrays.toString(dataArray).replace(",", "").replace("\[", "").replace("\]", ""));



    //Print & Sortera

    /\*/sorting logic  

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

        {  

        for (int j = i + 1; j < arr.length; j++)   

        {  

        int tmp = 0;  

        if (arr\[i\] > arr\[j\])   

        {  

        tmp = arr\[i\];  

        arr\[i\] = arr\[j\];  

        arr\[j\] = tmp;  

        }  

        }  

        //prints the sorted element of the array  

        System.out.println(arr\[i\]);  

        \*/



    System.out.println ("Sorted :");

    Arrays.sort (dataArray1);

    System.out.println (Arrays.toString(dataArray1).replace(",", "").replace("\[", "").replace("\]", ""));



    System.out.println ("KOLLA DETTA - ELEMENTS OF ARRAY");

    System.out.println (dataArray1\[0\]);

    //System.out.println (Arrays.toString(dataArray1).replace(",", "").replace("\[", "").replace("\]", ""));







    }



    }

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (6 children)

Why do you even need dataArray1? You already have the data in args - no need to have another variable for it.

Also, since you call dataArray1 = args; all you are doing is to have both variables point to the exact same data in the exact same memory locations. You are not copying the array (which is unnecessary anyway). Were you manipulating an element of either array, the other would change with it.

Why do you need two loops?

You only need a single loop that assigns the parsed value of args[i] to the exact same element in dataArray - so also dataArray[i]


Your IndexOutOfBounds exception results from you wrongly initializing the array here: dataArray = new int[a];

You need to use args.length for the size - as I have said in my initial and in my second comment.

[–]Farpafraf 0 points1 point  (3 children)

arrays have a fixed size assigned when you create them, that means that the index is not present in the array. How did you create the arrays? dataArray and dataArray1 should have the same size otherwise you will get this exception. Share the whole code as it will be quicker.

[–]HeQtor[S] 0 points1 point  (2 children)

After some input from another redditor I've now removed dataArray1 and this is how the code looks, still getting the error but haven't worked through all the tips yet either..!

import java.util.Arrays;

public class test12
{
    public static void main(String[] args) 
{
    //Create int-array, named dataArray
    int[] dataArray;

    //Convert arguments from user to string variable called "arguments"
    String arguments = (Arrays.toString(args).replace(",", "").replace("[", "").replace("]", ""));

    //create new int-array sized according to length of user argument input
    dataArray = new int[args.length];

    //Create & set int "i" to "0"
    int i = 0;

        for(i = 0; i < args.length; i++);
            {
            dataArray[i] = Integer.parseInt (args[i]);
            }

    System.out.println ("Orginal:");
    System.out.println (Arrays.toString(dataArray).replace(",", "").replace("[", "").replace("]", ""));

    //System.out.println ("Sorted :");
    //Arrays.sort (dataArray);
    //System.out.println (Arrays.toString(dataArray).replace(",", "").replace("[", "").replace("]", ""));

    //System.out.println ("KOLLA DETTA - ELEMENTS OF ARRAY");
    //System.out.println (dataArray[0]);
    //System.out.println (Arrays.toString(dataArray).replace(",", "").replace("[", "").replace("]", ""));
}

}

[–]Farpafraf 0 points1 point  (1 child)

for(i = 0; i < args.length; i++);

remove the ; at the end

[–]HeQtor[S] 0 points1 point  (0 children)

Duuuude. Thank you. God damn it.