I have an array it has (id,name,salary)
I want to search specific ID using Employee search method, my code is:
Employee SearchID(int i_d) {
for (int i = 0; i < staff.length; i++) {
boolean check = true;
if (staff[i].id == i_d) {
System.out.println("Id: " + staff[i].id + ", name: " + staff[i].name + " and salary: " + staff[i].salary);
} else {
System.out.println("Sorry, no record exists with record id = " + i_d);
}
}
return staff[i].id;
}
Your SearchID method returns an Employee object but you return a primitive type. (staff[i].id) You must change return type of your method.
You can fix your method like this:
Employee SearchID(int i_d) {
for (int i = 0; i < staff.length; i++) {
if (staff[i].id == i_d) {
System.out.println("Id: " + staff[i].id + ", name: " + staff[i].name + " and salary: " + staff[i].salary);
return staff[i];
}
}
System.out.println("Sorry, no record exists with record id = " + i_d);
return null;
}
And, of course, you need to handle the null result after calling SearchID properly.
Related
I'm learning Java for a week, and now i have a problem, because i want to print all Arraylist in method printArray, but the method don't see getName() and other methods and I don't know how to solve my problem. Thanks a lot for your help.
If you can, please show my what I;m doing wrong.
Class Positions:
public class Positions {
List<Positions> list = new ArrayList<>(15);
int ageAdd;
int IDAdd;
String nameAdd;
int counter;
String name;
int age;
int ID;
public Positions(String name, int age, int ID) {
this.name = name;
this.age = age;
this.ID = ID;
}
public Positions() {
this("", 0, 0);
}
//there are methods:
//adding element
//removing element
//changing values
//etc
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getID() {
return ID;
}
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.getName(i) + " AGE: " + list.getAge(i) + " ID: " list.getID(i));
}
}
Class main:
public class ArrayList2
{
public static void main (String[] args)
{
Positions p = new Positions();
System.out.println("----LISTA TABLICOWA-----");
System.out.println("Co chcesz wykonać? ");
System.out.println("1. Dodac element do listy. ");
System.out.println("2. Usunac elemnt z listy.");
System.out.println("3. Wstawić na dowolna pozycje.");
System.out.println("4. Rozmiar listy.");
System.out.println("5. Zmienic wartosc na podanym indeksie.");
System.out.println("6. Wyświetlić listę. ");
while(true) {
System.out.println("podaj pozycję: ");
Scanner ch = new Scanner(System.in);
int choice= ch.nextInt();
switch (choice)
{
case 1:
{
p.addPosition(); //?
break;
}
case 2:
{
p.removePosition();
break;
}
case 3:
{
p.setOnAnyPosition();
break;
}
case 4:
{
p.ArraySizeShow();
break;
}
case 5:
{
p.changePosition();
break;
}
case 6:
{
p.printArray();
break;
}
}
}
}
}
You simply have a problem with the array access:
Instead of using:
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.getName(i) ...);
}
}
You should use:
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.get(i).getName() ...);
}
}
because you want to get the name of a specific element, and in OO languages, that usually means calling the method on the object itself.
Ijn your example, you have a list that contains objects that contains a name. So if you want to access the name from the list, you need to first get an element then get his name.
HTH.
Try to change method with below code. Hope it would help.
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.get(i).getName() + " AGE: " + list.get(i).getAge() + " ID: " list.get(i).getID());
}
}
To loop over your list either use
for (int i = 0; i < list.size(); i++) {
Positions p = list.get(i);
System.out.println(i + " : " + " NAME: " + p.getName() + " AGE: " + p.getAge() + " ID: " + p.getID());
}
or
int index =0;
for (Positions p : list) {
System.out.println(index++ + " : " + " NAME: " + p.getName() + " AGE: " + p.getAge() + " ID: " + p.getID());
}
For this assignment I have to count the number of operations that my program does. I have put an OpCount counter in my loops etc., but am stumped on how to do so for an if statement. Heres my code for the ifs for some context (Printing details of a specific dam from an array of objects of type dam):
public void printDam(String damName) {
boolean find = false;
for (int i = 0; i<211; i++) {
ObjDam dam = data[i];
if (dam.getDamName().equals(damName)) {
System.out.println("Dam Name: " + dam.getDamName() +
", FSC: " + dam.getFsc() +
", Dam Level: " + dam.getDamLevel());
find = true;
}
}
if (find == false) {
System.out.println("The dam " + damName + " you entered cannot be found in the file");
}
}
Any help would be greatly appreciated.
I am trying to get an object (Space) and check if it overlaps an obstacle. if it does, i want to return a null, if not then return the space object. But i dont know how to do that. do i put a if statment at the end of the program outside of the for loop?
public Object checkob(Obstacle obstacle, Space space, int numofobst) {
int object = space.getX();
System.out.println("space location is " + object + " and the space is " + space.getName());
for (int ob = 0; ob < numofobst; ob++) {
if (object == obstacle.getX()) {
System.out.println("Space " + space.getName() + " is not able to be choosen");
return null;
}
System.out.println("Space " + space.getName() + " is able to be choosen");
return space;
}
}
I think you need to move return space outside of your loop
public Object checkob(Obstacle obstacle,Space space,int numofobst){
int object = space.getX();
System.out.println("space location is " + object +" and the space is " + space.getName());
for (int ob = 0; ob < numofobst; ob++){
if (object == obstacle.getX()){
System.out.println("Space " +space.getName() + " is not able to be choosen");
return null;
}
}
System.out.println("Space " +space.getName() + " is able to be choosen");
return space;
}
I’m working on a registry for persons and their belongings. This program is command based and therefore has some different functions. My problem here is that when the command ”show richest person" is performed, incorrect belongings of the calculated person are printed. The person and the total value of this owner’s items are correct; only the list of gadgets seem to be mixed up with someone else’s. This is how the code looks like:
public void getRichest() {
double maxValue = -1;
ArrayList<Person> richestPersons = new ArrayList<Person>();
for (int i=0; i<personRegistry.size(); i++) {
double totalValue = personRegistry.get(i).getTotalValue();
if (totalValue > maxValue) {
maxValue = totalValue;
richestPersons = new ArrayList<Person>();
richestPersons.add(personRegistry.get(i));
} else if (totalValue == maxValue)
richestPersons.add(personRegistry.get(i));
}
System.out.print("The richest person is ");
for (int i=0; i<richestPersons.size();i++) {
System.out.print(richestPersons.get(i).getName() + " ");
}
System.out.println("with the total value " + maxValue);
for (int i=0; i<richestPersons.size();i++) {
String name = richestPersons.get(i).getName();
ArrayList<Item> items = personRegistry.get(i).getItems();
// I GUESS THE PROBLEM STARTS HERE...
System.out.println(name + " owns the following items:");
for (int j=0; j<items.size(); j++) {
String itemName = items.get(j).getName();
double itemValue = items.get(j).getValue();
System.out.println(itemName + " " + itemValue + " ");
}
}
}
The described problem only occurs in the execution of this command and no other. For example, If I perform a command called ”show information about specific person” and select the same person (the richest) the belongings of this person will now be correct. The code is almost identical with the earlier shown:
}
public void search(String name) {
for (int i=0; i<personRegistry.size(); i++) {
if (name.equalsIgnoreCase(personRegistry.get(i).getName())) {
String foundName = personRegistry.get(i).getName();
double totalValue = personRegistry.get(i).getTotalValue();
System.out.println(foundName + " has a total value of " + totalValue + " ");
System.out.println(foundName + " owns the following items:");
ArrayList<Item> items = personRegistry.get(i).getItems();
for (int j=0; j<items.size(); j++) {
String itemName = items.get(j).getName();
double itemValue = items.get(j).getValue();
System.out.println(itemName + " " + itemValue + " ");
}
return;
}
}
System.out.println("Error: Could not find the person you were searching for.");
}
I’ve tried to figure this out (for example, I've tried copy-paste the latter code into the "getRichest"), but I would really appriciate some help!
If you're simply trying to print out only the richest person and their belongings, you should use:
Person richest = new Person(/*Any default initializers here*/);
// This will set 'richest' to the richest person
for (Person p : personRegistry) {
if (p.getValue() > richest.getValue())
richest = p;
}
System.out.println("Richest person is: " + richest.getName());
System.out.println("Total value: " + richest.getTotalValue());
System.out.println(richest.getName() + " owns: ");
for (Item i : richest.getItems()) {
System.out.println(i.getName() + " " + i.getValue());
}
If you wish to hold duplicates (that is, Person objects with the same totalValue),
ArrayList<Person> richestPeople = new ArrayList<Person>();
Person richest = new Person(/*Any default initializers here*/);
for (Person p : personRegistry) {
if (p.getValue() > richest.getValue())
richest = p;
}
// now check to see if there are people with same value
for (Person p : personRegistry) {
if (p.getValue() == richest.getValue())
// add them to the list
richestPeople.add(p);
}
// now we loop through the list of richest people and print them out
for (Person p : richestPeople) {
System.out.println("Richest person is: " + p.getName());
System.out.println("Total value: " + p.getTotalValue());
System.out.println(p.getName() + " owns: ");
for (Item i : p.getItems()) {
System.out.println(i.getName() + " " + i.getValue());
}
}
I'm creating a booking system in Java to prevent double bookings i have created a for loop that should change a Boolean to booked once the booking is made however it is changing all the bookings to booked when i only want one instance of booking so no one else can make a booking.
public static boolean booked;
private void FSubmitActionPerformed(java.awt.event.ActionEvent evt) {
for ( int i = 0; i < Airplane.Fseat.length; i++)
{
String seat = FCol.getSelectedItem().toString() + FRow.getSelectedItem().toString();
String items = Snack.getSelectedItem().toString() + " " + Drink.getSelectedItem().toString();
Airplane.Fseat[i] = seat;
Airplane.item[i] = items;
if (Airplane.Fseat[i] != null)
{
System.out.println("Seat number is First class " + Airplane.Fseat[i].toString() + "\n" +"Food and drink " + " " + Airplane.item[i].toString());
i++;
}
else
{
System.out.println("Cannot book already taken");
}
}
You have not put any condition to check, how a particular seat will be set selected.
So you will need to modify your code as:
for ( int i = 0; i < Airplane.Fseat.length; i++)
{
String seat = FCol.getSelectedItem().toString() + FRow.getSelectedItem().toString();
String items = Snack.getSelectedItem().toString() + " " + Drink.getSelectedItem().toString();
if(your_condition_to_check_if_this_seat_is_selcted ){
Airplane.Fseat[i] = seat;
Airplane.item[i] = items;
}
if (Airplane.Fseat[i] != null)
{
System.out.println("Seat number is First class " + Airplane.Fseat[i].toString() + "\n" +"Food and drink " + " " + Airplane.item[i].toString());
i++;
}
else
{
System.out.println("Cannot book already taken");
}
}