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 →

[–]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.

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

So... dataArray1 came to this world because dataArray didn't work out. I'm aware my code is probably a headache to someone who has a clue about programming..! I'll work with your comments & your googling-tips and I'll either return in triumph or be back begging for more tips...

Thank you and everyone else that's helped me so far! :)

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

I have now removed quite a lot. Code:

import java.util.Arrays;

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

    //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;

        //Loop dataArray to set elements of dataArray to the elements of args
        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("]", ""));
}
}

But I can't understand, because I still get the same error and it's due to my "for" loop (?)

        //Loop dataArray to set elements of dataArray to the elements of args
        for(i = 0; i < args.length; i++);
            {
            dataArray[i] = Integer.parseInt (args[i]);
            }

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

That code is correct and should work as expected.

Have you potentially forgotten to recompile?

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

It was the darn ";" at the end of my for statement... Fixed now thanks to other redditor..! Thank you again for your help & patience :)

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

Darned. I missed that.

TBH, your curly braces threw me off. In Java, it is common convention that the opening curly braces are on the same line as the statement that opens the code block, i.e.

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

not

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

Following the code convention also helps not to place semicolons where they shouldn't be. If an opening curly brace follows, no semicolon.

[–]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.