Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-27-2007, 06:14 PM   #1 (permalink)
 
Newb Techie

Join Date: Mar 2007

Location: South of Ireland

Posts: 35

mitzi is on a distinguished road

Default n00b - java array

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();
			}
		}
		
	}

mitzi is offline  
Old 03-27-2007, 06:40 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default Re: n00b - java array

First - one question:

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


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

Last edited by Vormund; 03-27-2007 at 06:41 PM. Reason: Email Notification
Vormund is offline  
Old 03-27-2007, 07:05 PM   #3 (permalink)
 
Newb Techie

Join Date: Mar 2007

Location: South of Ireland

Posts: 35

mitzi is on a distinguished road

Default Re: n00b - java array

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

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??
mitzi is offline  
Old 03-27-2007, 07:36 PM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default Re: n00b - java array

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...
__________________
Vormund is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On