• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array problems  RSS feed

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goodness I hate arrays!! Something to make coding simple is completely hard for me to get correct lol Anyways I seriously need help. My arrays are out of bounds. All i want to do is have the user enter employee information which is placed in the array(s). The code is not finished, the while loop is there because the user can quit anytime but they cannot enter more than 100 employees. SO excuse the while loop that's the least of my worries. Please please please help my instructor hasn't responded back in 4 days -_- and no tutors are available, I'm seriously stranded...

Heres the driver:

import java.util.Scanner;

public class EmployeeDemo
{
public static void main(String[] args)
{
Employee[] myEmp = new Employee[100];
boolean done = false;

Scanner kboard = new Scanner(System.in);

while(!done)
{
for(int i = 0; i < 5; i++)
{
System.out.println("Enter employee name:");
myEmp[i].setName(kboard.nextLine());
System.out.println("Enter employee salary:");
myEmp[i].setSalary(kboard.nextDouble());
System.out.println("Enter employee SSN:");
myEmp[i].setSSN(kboard.nextInt());
}
}
}
}



 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the employee class:


public class Employee
{
Scanner kboard = new Scanner(System.in);
public double salary;
public int ssn;
public String name;

Employee()
{
name = "Unknown Name";
salary = 0.0;
ssn = 0;
}

Employee(String instName, double instSalary, int instSSN)
{
name = instName;
salary = instSalary;
ssn = instSSN;
}

Employee(String instName)
{
name = instName;
}

Employee(double instSalary)
{
salary = instSalary;
}

Employee(int instSSN)
{
ssn = instSSN;
}

// Set Mutators
public void setName(String newName)
{
name = newName;
}

public void setSSN(int newSSN)
{
ssn = newSSN;
}

public void setSalary(double newSalary)
{
salary = newSalary;
}

// Set accessors
public String getName()
{
return name;
}

public int getSSN()
{
return ssn;
}

public double getSalary()
{
return salary;
}
}
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Avilez wrote:My arrays are out of bounds.


What makes you think this? To me your problem looks like something else; do you have a stack trace which mentions "out of bounds"? Perhaps you could post the stack trace if you would like help in interpreting it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem in your class EmployeeDemo: You're creating an array of Employee objects, but that array will initially be empty - it will contain 100 empty slots for Employee objects.

You'll get a NullPointerException when you try to do myEmp[i].setName(...) because myEmp[i] is null at that point. You have to create a new Employee object first and put that into myEmp[i] before you try to call methods on it.
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, I've been dealing with that all day I kinda assumed that was the problem when the program wouldn't start. I double checked and it says that there is a nullPointerException in my main class.
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong
So something like this?:

Employee Object = new Employee();

for(--------)
{
Object.setName(kboard.nextLine);
myEmp[i] = Object.getName();

}

I hope this is right I'll feel good about it =)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not entirely correct.

First of all, you want to create a new Employee object each time you go through the loop. So creating the Employee object should be inside the loop, not outside the loop.

Second, you want to set myEmp[i] to the new Employee object. Not to the Employee object's name, as you're proposing with myEmp[i] = Object.getName();.

Don't call the variable Object, because the name Object is the name of Java's class java.lang.Object.
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dude finally got it! =) I can finally move on to that whille loop. I'm already having trouble with it but I'm sure I can find some examples online hoepfully
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!