n00b - java array

Status
Not open for further replies.

mitzi

Baseband Member
Messages
53
Hi all,

am new here - yay for my first post!

Am having a small bit of trouble with the following code...it inputs and seems to work fine until it comes to printing the contents of the array. All it seems to return is null. This is an example of the output I get:


Please enter the number of employees:
1
Please enter employee #1:
Jenny
Enter their extension number:
4578
null

null


Code:
import java.util.Scanner;
public class testArray
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		int numEmployees;
		
		
		System.out.println("Please enter the number of employees: ");
		numEmployees = scan.nextInt();
		scan.nextLine();
		
		String[] employee = new String[numEmployees];
		String[] telExtension = new String[numEmployees];
		String[][] employeeDetails = new String[numEmployees][numEmployees];
		
		int index=0;
		for(index=0; index < numEmployees; index++)
		{
			System.out.println("Please enter employee #" + (index+1) + ": ");
			employee[index] = scan.nextLine();
			System.out.println("Enter their extension number: ");
			telExtension[index] = scan.nextLine();
		}
		
		
		for(int emp=0; emp < employee.length; emp++)
		
			for(int tel=0; tel < telExtension.length; tel++)
		{
				employee[emp] += employeeDetails[emp][tel];
				telExtension[tel] += employeeDetails[emp][tel];
				System.out.println(employeeDetails[emp][tel]);
				System.out.println();
		}
			
		for(int emp=0; emp < employee.length; emp++)
		{
			for(int tel=0; tel < telExtension.length; tel++)
				System.out.println(employeeDetails[emp][tel]);
		 System.out.println();
			}
		}
		
	}
 
First - one question:

Why is "scan.nextLine();" there? Acting as a newline? :p


And why is it null? It doesn't look like employeeDetails[][] is ever populated with any data. So you would need to either change the way the data is stored when it's "scanned" or change your for-loop from:

Code:
		for(int emp=0; emp < employee.length; emp++)
		
			for(int tel=0; tel < telExtension.length; tel++)
		{
				employee[emp] += employeeDetails[emp][tel];
				telExtension[tel] += employeeDetails[emp][tel];
				System.out.println(employeeDetails[emp][tel]);
				System.out.println();
		}

to

Code:
for ( int i = 0; i < employee.length; i++ )
{
	System.out.println( "Employee: " + employee[i] + " Extension: " + telExtension[i] );
}

Or you could do as you are, without the employee/telExtension array, and just populate the employeeDetails as you receive the input.

I doubt this makes any sense, but feel free to ask any Q or tell me how much I misunderstood...;)
 
Hi Vormund :)

Thanks for the quick reply!! Makes perfect sense :)

-puts head in hands-

Looking back at my code now, I really don't know why I tried to make it so complicated for myself. I had it stuck in my head to use two dimensional arrays and I didn't really need to!

Thanks very much for your help - much appriciated :D

As for the scan.nextLine() I stuck in there...if I didn't have that in..it won't let me enter in the first employees name and the output gets all screwed up. Is there a better way to do this??
 
Hah, I just started using Java too...'tis all good!


scan.nextLine(), in your program, is actually just skipping the line, as what it's doing is:

scan.nextInt() gets the integer
scan.nextLine() captures all data until it hits a newline character, returning anything it found
->theoretically nextLine() is unreachable in terms of capturing data, as nextInt() would either get the int, or throw an type mismatch exception

Bottom line...try replacing it with
System.out.println();
! That should work dandy.


Also, if you didn't know, there's also System.out.print(); which is exactly the same as println except it doesn't print the newline (\n) character. So if you wanted to have it like:
Please enter the number of employees: 1
Please enter employee #1: Jenny
Enter their extension number: 4578
You'd do something like

System.out.print("Please enter the number of employees: ");
int number = scan.nextInt( );
System.out.println( );
.
.
System.out.print("Please enter employee #1");
String employeescan.nextLine( );

etc. By the by, I'm very surprised you made any sense of it... as I am terrible at explaining these things... :)
 
Status
Not open for further replies.
Back
Top Bottom