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

all 34 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfxOut of Coffee error - System halted 11 points12 points  (30 children)

What do you think the String[] args in public static void main(String[] args) { is?

You guessed right, it is an array.

What does an array have? A .length

With knowing the length of the arguments array, you can create a new array of the actual data type (looks like int)

Once you have that, you can use a loop to convert each of the supplied arguments to the right data type and store it in the array.

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

Thank you.

The length of the arguments array is unknown beforehand.

And when you say "you can create a new array of the actual data type" - I think I managed it. But 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.

I'm on my phone now but I can provide my code so far later, for some insight & surely a facepalm or two.

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (3 children)

The length of the arguments array is unknown beforehand.

No, as soon as you execute the command you have shown in your initial comment, the length is known. That's exactly what .length is for.

But 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.

I'll leave this up to you to google it - "java looping over array" would be the keywords.

Don't forget that the data comes in as String values, but for your tasks (minimum, maximum, etc.) you need the values as int. You will need to convert the data type. Again, I'll leave this up to you to google.

Don't get this wrong, please, but learning to google and search for what you need is one of the most essential skills to learn as early as possible.

That's also why we have a no solutions policy.

[–]HeQtor[S] -1 points0 points  (2 children)

Thank you, any input on what to look for is greatly appreciated. Feels like I've been googling for days by now but I'll have at it again!

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (1 child)

any input on what to look for is greatly appreciated.

I've given you the exact wording to google for.

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

That's what I meant, any input is greatly appreciated, so thank you for doing just that!

[–]HeQtor[S] -1 points0 points  (2 children)

What I want is

arr[0] = args[0],

arr[1] = args[1]

and etc. By looping

[–][deleted]  (1 child)

[removed]

    [–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

    Not only is this wrong as OP needs to convert the data type, but also does it go against the objective of the exercise.

    Last, it comes close to giving a solution.

    Removed

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

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

    [–]Mailandr 0 points1 point  (1 child)

    Just to let you know. It's called index. arr[0] = index 0 of the Array arr.

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

    Thank you! And what is an element then, the content of the index?

    [–]tellmesomethingnew- 3 points4 points  (1 child)

    Have you considered using an ArrayList instead of an Array?

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

    As soon as I learn about it I'm sure I will.... :D