2

This is my first question on this site, I'm running this on NetBeans 8.0.2 and trying to print out my user-defined array but it keeps returning null values. For example if you say there are 2 employees and enter both of their names, it will return [null, null]

How to fix this error? I'm a novice.

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Arrays;

class Tips_Calculation2 
{
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

    System.out.print("How many employees for the week?: ");
    int numberOfEmps = scan.nextInt();

    // counter for the if statement that should return all employees from the array
    int counter = numberOfEmps;

    int[] nOEarray = new int[numberOfEmps];


    System.out.println("\nEnter names of workers up to the entered amount (" + numberOfEmps + "):");


    for(int i = 1; i <= numberOfEmps; i++)
    {
        String nameCycler = scan.next();
        String[] namesArray = new String[i];

        if(counter == i)
        {
            System.out.println(Arrays.toString(namesArray));
        }
    }
}
}

Disregard import java.text.DecimalFormat as I plan to use this import later on in my code. Thank you in advance to anyone who is kind/smart enough to respond.

Ryan Horner
  • 123
  • 1
  • 1
  • 7

7 Answers7

1

First of all you never put your nameCycler to array. Second of all you create your namesArray every iteration which I think is wrong.

wawek
  • 1,577
  • 1
  • 13
  • 23
1

You're creating a brand new (full of null) array namesArray on every pass through the loop--and then never assigning anything to it. I think you're looking for something like this instead. Note that Java indexes from zero, not one.

String[] names = new String[numberOfEmps]

for(int i = 0; i < names.length; i++) {
    names[i] = scanner.next();
}

System.out.println(Arrays.toString(names));
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

First of all, you should initialise the array outside of your loop. Secondly, you forgot to set the name to the array value(s).

Try this:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Arrays;

class Tips_Calculation2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("How many employees for the week?: ");
        int numberOfEmps = scan.nextInt();

        int[] nOEarray = new int[numberOfEmps];

        System.out.println("\nEnter names of workers up to the entered amount (" + numberOfEmps + "):");

        String[] namesArray = new String[numberOfEmps];

        for (int i = 0; i < numberOfEmps; i++) {
            namesArray[i] = scan.next();
        }
        System.out.println(Arrays.toString(namesArray));
    }
}
Matthias A. Eckhart
  • 5,136
  • 4
  • 27
  • 34
0

You never assign the name to the array and in every iteration you define the array new:

 String[] namesArray = new String[numberOfEmps];
 for(int i = 1; i <= numberOfEmps; i++)
    {
        String nameCycler = scan.next();
        namesArray [i] = nameCycler ;
        if(counter == i)
        {
            System.out.println(Arrays.toString(namesArray));
        }
    }
Jens
  • 67,715
  • 15
  • 98
  • 113
0

Added comments in the code to point out changes.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.print("How many employees for the week?: ");
    int numberOfEmps = scan.nextInt();

    // removed 'nOEarray' and 'counter'

    if (numberOfEmps > 0) {
        System.out.println("\nEnter names of workers up to the entered amount (" + numberOfEmps + "):");

        // initializing 'namesArray' outside for loop.
        String[] namesArray = new String[numberOfEmps];
        for(int i = 0; i < numberOfEmps; i++) { // initialized with 0 and updated condition with '<'
            namesArray[i] = scan.next(); // assigning value to 'i'th position of namesArray
        }
        System.out.println(Arrays.toString(namesArray)); // Printing array outside for loop
    }
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

Replace

for(int i = 1; i <= numberOfEmps; i++)
{
    String nameCycler = scan.next();
    String[] namesArray = new String[i];

    if(counter == i)
    {
        System.out.println(Arrays.toString(namesArray));
    }
}

With

String[] namesArray = new String[numberOfEmps];
for(int i = 0; i < numberOfEmps; i++)
{
    namesArray[i] = scan.next();
}
System.out.println(Arrays.toString(namesArray));

And see whether it works.

Ferdinand Neman
  • 680
  • 3
  • 6
-1

"How to fix this error" not. This is not an error.

 String[] namesArray = new String[i]; // step one, you declare an array of Strings
  // which you don't initialize

        if(counter == i)
        {
            System.out.println(Arrays.toString(namesArray));
//you print all the (non-initialized) elements of namesArray
//since you didn't initialize the elements, it takes the default value, which is null
        }

fill the elements of the array with Strings before trying to print them.

Stultuske
  • 9,296
  • 1
  • 25
  • 37