-4
import java.util.Scanner;
import java.util.Arrays;

class Main
{
    public static void main(String[] args)
    {
        Scanner amount = new Scanner(System.in);
        System.out.print("How many names would you like to enter?: ");
        int total = amount.nextInt();
        String[] mainArray = new String[total];
        for (int i=0; i<total; i++){
        Scanner names = new Scanner(System.in);
        System.out.print("Gimme a name!: ");
        String name = names.nextLine();
          mainArray[i]=mainArray[i]+name.charAt(0);
        }
        System.out.println(Arrays.toString(mainArray)+"");
    }
}

Input: 3 Input: Moe Input: Curly Input: Vader

Returns

How many names would you like to enter?: 3
Gimme a name!: Moe 
Gimme a name!: Curly
Gimme a name!: Vader
[nullM, nullC, nullV]

The code should return "MCV", no spaces. I have no clue why it's printing Null beforehand when it prints the specified character right after, it should print after the Array is filled, or at least I thought it should.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
MattScott
  • 11
  • 1
  • 3
    `mainArray[i]=mainArray[i]+name.charAt(0);` What value do you think is stored in `mainArray[i]` at this point? – UnholySheep Jan 08 '21 at 23:23
  • or maybe a better question is what is in mainArray[i] when you start? – Petter Friberg Jan 08 '21 at 23:24
  • A small tip: you don't have to create new scanners for reading different input. You can use amount to read in the names. You should also close your scanners after reading them with "amount.close() and names.close()" – Yolomep Jan 08 '21 at 23:24
  • I thought it would store the first character of the Scanner input. Ex input Moe, mainArray[0] would be M – MattScott Jan 08 '21 at 23:24
  • `mainArray[i]=name.charAt(0);` – aran Jan 08 '21 at 23:25
  • 1
    `mainArray[i]=mainArray[i]+name.charAt(0);` is the same as `mainArray[i]=null+name.charAt(0);` in your code. Do you really think that this is what you want to do? – UnholySheep Jan 08 '21 at 23:25
  • A good rule of thumb for programming is to first try to explain everything to the computer in English (or your native language), so you get the logic ironed out. Then you can translate that into computer code. There's nothing wrong with the code you've written, it just doesn't do what you want it to because you got the logic wrong. Flow charts can also be really helpful. – Charlie Armstrong Jan 08 '21 at 23:30
  • 1
    Do not close any Scaner that uses System.in before the end of the program. Once you close such a scanner, it will close System.in, and good luck reopening that. – NomadMaker Jan 08 '21 at 23:34
  • Yep @NomadMaker, sorry. You should close a scanner after reading it *unless* its System.in – Yolomep Jan 09 '21 at 02:17

1 Answers1

0

You can use a variable of type StringBuilder or String or StringBuffer etc. to append the desired values to. Currently, you are appending the values to the array elements which have been initialized as null. Moreover, you do not need multiple instances of Scanner (and never in a loop).

Demo:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many names would you like to enter?: ");
        int total = Integer.parseInt(scanner.nextLine());
        String[] mainArray = new String[total];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < total; i++) {
            System.out.print("Gimme a name!: ");
            mainArray[i] = scanner.nextLine();
            if (mainArray[i].length() >= 1) {
                sb.append(mainArray[i].charAt(0));
            }
        }
        System.out.println(Arrays.toString(mainArray));
        System.out.println(sb);
    }
}

A sample run:

How many names would you like to enter?: 3
Gimme a name!: Moe
Gimme a name!: Vader
Gimme a name!: Curly
[Moe, Vader, Curly]
MVC

Note that I have used Integer.parseInt(scanner.nextLine()) instead of scanner.nextInt() so that the new line character entered after the integer input is not left to be consumed by the next scan. Check this discussion to learn more about it.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72