-3
import java.util.*;
public class RandomAddArray {
    public static void main (String[] args) {
        AddArray ad = new AddArray();
        int[] Ar = new int[4];
        ad.AddArray(Ar);
    }
}

class AddArray {
    public void AddArray(int a[]) {
        for(int i = 0; i < a.length; i++) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            a[i] = n + 2;
            System.out.print(a[i]);
        }
    }
}

In my code I read four integers from the console and add 2 to each of them. If I type number 1 four times, System.out.print should output 3 four times.

However, I get the following output:

Console output

thatguy
  • 13,242
  • 6
  • 19
  • 33

2 Answers2

0

If you call System.out.print(value); after retrieving your input, the value will be printed directly after your input (the last character is the newline character, hence in the next line). Just put it in a separate loop after the first loop like this:

public void AddArray(int a[]) {

    Scanner sc = new Scanner(System.in);

    for(int i = 0; i < a.length; i++) {
        int n = sc.nextInt();
        a[i] = n + 2;
    }

    for (int value : a) {
        System.out.print(value);
    }

}

Then, it will print 3333 in a separate line after your input. If you want to print each number in a separate line, use System.out.println(value); instead.

thatguy
  • 13,242
  • 6
  • 19
  • 33
  • Thanks! It works perfectly! But I have one question to you. What does : mean in for (int value : a) ? – javaprogrammer Nov 26 '16 at 14:00
  • @javaprogrammer It is an for each loop and means for every value in the array `a`. `int value` it changes for each value in `a` . – marpme Nov 26 '16 at 14:04
0

Here is my current solution. Hopefully the commentary helps you out a bit, otherwise feel free to ask me some further questions. :)

Greetings Kyon

import java.util.*;

public class Main {

    public static void main (String[] args) {
        // create Array to fill and pass it into our fill function
        int[] Ar = new int[4];
        AddArray.addToArray(Ar);
        System.out.println(Arrays.toString(Ar));
    }

    private static class AddArray {

        // static class, there is no need to instantiate it.
        public static void addToArray(int a[]) {

            // create on scanner out of the loop
            Scanner sc = new Scanner(System.in);

            // for each array index let's scan for some new int's
            for(int i = 0; i < a.length; i++) {
                System.out.printf("Type in your %s of %s integer:%n", i+1, a.length);
                int n = sc.nextInt();
                a[i] = n + 2;
            }

            // close scanner afterwards
            sc.close();
        }

    }

}

The output will be:

Type in your 1 of 4 integer:
4
Type in your 2 of 4 integer:
124
Type in your 3 of 4 integer:
12
Type in your 4 of 4 integer:
2
[6, 126, 14, 4]

Process finished with exit code 0
marpme
  • 1,667
  • 12
  • 21
  • Oh your output looks really awesome.. Thanks for your great advice!! – javaprogrammer Nov 26 '16 at 14:14
  • @Kyon You should not close a `Scanner` on a `System` stream. Why? Because it is the responsibility of the JVM, which **created** it. – thatguy Nov 26 '16 at 14:33
  • @thatguy I close the stream, because it's not longer needed in that case, so there no need to keep the stream open. Referring to the Java doc scanners are being closed there aswell. https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html – marpme Nov 26 '16 at 14:58
  • 1
    Right, but only the `Scanner` on the strings are closed, not on `System.in`. You should never close resources, you did not open, which includes `System.in`. That would be bad coding practice. I made the mistake myself, as e.g. Eclipse gives you a **false** resource leak warning. For more information, look [here](http://stackoverflow.com/questions/23621668/java-closing-scanner-and-resource-leak) and [here](http://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in). Another problem in later use would be, that you cannot reopen `System.in`. – thatguy Nov 26 '16 at 15:20