简体   繁体   中英

How do I get a string user input and increase value by +1 using Java ArrayList?

I have the following code and I want to ask the user after an name that's already stored in the Arraylist and increase that (dogs) age by +1.

Here's the most important (I guess) part of the code. This is the Register Class.

import java.util.Scanner;
import java.util.ArrayList;

public class Register {

private Scanner keyboard = new Scanner(System.in); 
private ArrayList<Dog> dogs = new ArrayList<>();


private String readString() {
    return keyboard.nextLine();
}

public int readInt() {
    int i = keyboard.nextInt();
    keyboard.nextLine();
    return i;
}

public double readDouble() {
    double d = keyboard.nextDouble();
    keyboard.nextLine();
    return d;
}


public void registrateDog(){ 

    System.out.println("Dogs name: ");
    String name = readString();

    System.out.println("Dogs breed: ");
    String breed = readString();

    System.out.println("Dogs age: ");
    int age = readInt();

    System.out.println("Dogs weight: ");
    double weight = readDouble();

    Dog newDog = new Dog(name, breed, age, weight);
    dogs.add(newDog);

    System.out.println(newDog.toString() + " is added");

  }



public void increaseAge(){ //Here's the problem

    System.out.print("Enter dog who has aged: ");
    String newDogAge = readString();

    int addAge = 1;

    for (int i = 0; i < dogs.size(); i++) { 
        if(dogs.get(i).getName().equals(newDogAge))

        addAge = (dogs.get(i).getAge());
        dogs.set(i, dogs.get(i));


        System.out.println("Dog " + newDogAge + " is now " + addAge);
        return;

    }
    }

private void exitProgram(){
    System.out.println("Goodbye!");
    keyboard.close();


}

private void run(){
    setUp();
    runCommandLoop();
    exitProgram();

}

public static void main(String[] args){

    new Register().run();
    //setUp();
}


}

And

public class Dog {

private String name;
private String breed;
private int age;
private double weight;
private double tailLenght;
private String tax = "tax";

public Dog(String name, String breed, int age, double weight){
this.name = name;
this.breed = breed; 
this.age = age;
this.weight = weight;
if (breed.equals(tax)) {
    this.tailLenght = 3.7;
} else {
    this.tailLenght = (age*weight) / 10;
}
}



public String getName(){
    return name;
}

public void setName(String name) {
this.name = name;
}

public String getBreed(){
    return breed;
}

public int getAge(){
    return age;
}

public void setAge(int age){
    this.age = age;
}

public int newAge(){
    return age = age + 1;
}



public double getWeight(){
    return weight;
}

public double getTailLenght(){
return tailLenght;
}

public String toString()
   { return String.format("%s, %s, %d years old, %.1f kg, "
    + "tail lenght %.1f cm", name, breed, age, weight, tailLenght);  
   }
}

So in this code you're finding the Dog by its name. Which is great.

public void increaseAge() {
    System.out.print("Enter dog who has aged: ");
    String newDogAge = readString();
    int addAge = 1;

    for (int i = 0; i < dogs.size(); i++) { 
        if(dogs.get(i).getName().equals(newDogAge))
            addAge = (dogs.get(i).getAge());
        dogs.set(i, dogs.get(i));
        System.out.println("Dog " + newDogAge + " is now " + addAge);
        return;
    }
}

Inside of your Dog class you have a method which returns the age, what you want to do is modify it to:

public int newAge(){
    this.age++; // This will take the current age of the dog and add one to it
}

Now going back to the increaseAge() method you want to modify the for loop to look like:

for (int i = 0; i < dogs.size(); i++) { 
    if(dogs.get(i).getName().equals(newDogAge))
        dogs.get(i).newAge(); // This will update the Dogs age by 1 year
        System.out.println("Dog " + dogs.get(i).getName() + " is now " + dogs.get(i).getAge());
        return;
}

Where are you incrementing the age ? Call the newAge() method as you have defined in the Dog class.`

     Dog dog = dogs.get(i);
     dog.setAge(dog.newAge());
     dogs.set(i,dog);`

We've fetched the particular dog who's details matched. Then we incremented it's age and set it back in the list at that position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM