0

I'm trying to use a key field with my array with Java but I'm confused on where to begin. My program is supposed to be that the user enters a name which would be the unique identifier and all this information would show up relating to the name kind of a mini Google. I have two classes, one is my abstract class and my other would be my main method class. If anyone could help me that would be great and very much appericated.

Here is my class.

import java.util.HashMap;
public class DataBase {

private  String name;
private  int year;
private  String status;
private  String habitat;
private  String lifespan;

public DataBase() {
}

HashMap list=new HashMap();
public DataBase(String name, int year, String status, String habitat, String lifespan) {
    this.name = name;
    this.year = year;
    this.status = status;
    this.habitat = habitat;
    this.lifespan = lifespan;
}


public  String getName() {
    return name;
}

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

public  int getYear() {
    return year;
}

public  void setYear(int year) {
    this.year = year;
}

public  String getStatus() {
    return status;
}

public  void setStatus(String status) {
    this.status = status;
}


 public  String getHabitat() {
    return habitat;
}

public  void setHabitat(String habitat) {
    this.habitat = habitat;
   }


 public  String getLifeSpan() {
    return lifespan;
}

public  void setLifeSpan(String lifespan) {
   this.lifespan = lifespan;
}


    public String toString()
{
    String result = "";
    result += "\nName: "+ getName() + "\nYear: "+getYear()+ "\nStatus: "+getStatus()+ "\nHabitat: "+getHabitat()+"\nLifeSpan: "+getLifeSpan();
    return result;
}

}

Here is my main method class.

import java.util.HashMap;
import java.util.Scanner;
public class DB
{
public static void main(String[] args) 
 {  
    Scanner in = new Scanner (System.in);

    String fName;
    System.out.print("Enter Name:");
    fName=in.next();

    String name;
    name=fName+" ";

    System.out.println(name);


    DataBase turtle1 = new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years");
    DataBase turtle2 = new DataBase("Galápagos tortoise", 1535, "Vulnerable"," Galápagos Islands","At least 100 years");
    DataBase turtle3 = new DataBase("West African mud turtle", 1812, "Least Concern","West and Central Africa","25 years");
    DataBase turtle4 = new DataBase("Black softshell turtle", 1875, "Extinct in the Wild","South Asia","Unknown");    
    DataBase turtle5 = new DataBase("Radiated tortoise", 1802, "Critically Endangered","Southern Madagascar","At least 150 years");
    DataBase turtle6 = new DataBase("Painted terrapin", 1844, "Critically Endangered","East Asia","Unknown");
    DataBase turtle7 = new DataBase("Philippine pond turtle", 1920, "Critically Endangered ","Philippines","Unknown");
    DataBase turtle8 = new DataBase("Flattened musk turtle", 1955, "Critically Endangered","Southern US","20-50 years");
    DataBase turtle9 = new DataBase("McCord's box turtle", 1988, "Critically Endangered","China","Around 50 years");
    DataBase turtle10 = new DataBase("Leatherback sea turtle", 1761, "Vulnerable","Atlantic and Pacfic Oceans","Around 30 years");

    System.out.println(turtle1);

    HashMap<String,DataBase> turtles = new HashMap<>();
    turtles.put("Diamondback terrapin", new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years"));
    turtles.put("Galapagos tortoise", new DataBase("Galapagos tortoise", 1535, "Vulnerable"," Galapagos Islands","At least 100 years"));
    turtles.put("West African mud turtle", new DataBase("West African mud turtle", 1812, "Least Concern","West and Central Africa","25 years"));
    turtles.put("Black softshell turtle", new DataBase("Black softshell turtle", 1875, "Extinct in the Wild","South Asia","Unknown"));
    turtles.put("Radiated tortoise", new DataBase("Radiated tortoise", 1802, "Critically Endangered","Southern Madagascar","At least 150 years"));
    turtles.put("Painted terrapin", new DataBase("Painted terrapin", 1844, "Critically Endangered","East Asia","Unknown"));
    turtles.put("Philippine pond turtle", new DataBase("Philippine pond turtle", 1920, "Critically Endangered ","Philippines","Unknown"));
    turtles.put("Flattened musk turtle", new DataBase("Flattened musk turtle", 1955, "Critically Endangered","Southern US","20-50 years"));
    turtles.put("McCord's box turtle", new DataBase("McCord's box turtle", 1988, "Critically Endangered","China","Around 50 years"));
    turtles.put("Leatherback sea turtle", new DataBase("Leatherback sea turtle", 1761, "Vulnerable","Atlantic and Pacfic Oceans","Around 30 years"));




    DataBase database = turtles.get("Diamondback terrapin");
    DataBase databas = turtles.get("Galapagos tortoise");
    DataBase databse = turtles.get("West African mud turtle");
    DataBase databa = turtles.get("Black softshell turtle");
    DataBase dataase = turtles.get("Radiated tortoise");
    DataBase datab = turtles.get("Painted terrapin");
    DataBase datase = turtles.get("Philippine pond turtle");
    DataBase datae = turtles.get("Flattened musk turtle");
    DataBase dataae = turtles.get("McCord's box turtle");
    DataBase datbase = turtles.get("Leatherback sea turtle");
    System.out.println(turtles.get(name));



}

}

UPDATE:

I can print out both the name and the information but I don't know how to connect the two together.

9
  • Look into java.util.Map and java.util.HashMap for a hint. Commented Dec 9, 2017 at 2:48
  • @DeleTaylor would I put that into my abstract class or main method class? Commented Dec 9, 2017 at 2:51
  • You'll need to replace your ArrayList with a HashMap and use the database name as the key. Commented Dec 9, 2017 at 2:58
  • Database is not an abstract class. private String[] platforms and `ArrayList list = new ArrayList();``is not needed. Use HashMap<String, Database>, where key is name. Commented Dec 9, 2017 at 5:22
  • 1
    For better help consider posting minimal reproducible example Commented Dec 9, 2017 at 5:30

2 Answers 2

1

I think what you are trying to do is something like the following:

import java.util.HashMap;

public class DataEntry {

    private  String name;
    private  int year;

    public DataEntry(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public  String getName() {  return name; }

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

    public  int getYear() { return year;}

    public  void setYear(int year) {    this.year = year;}

    @Override
    public String toString()
    {
        return "\nName: "+ getName() + "\nYear: "+getYear();
    }

    public static void main(String[] args)
     {
        //create 2 entries
        String name1 = "Diamondback terrapin", name2 = "Galápagos tortoise";
        DataEntry turtle1 = new DataEntry(name1, 1793);
        DataEntry turtle2 = new DataEntry(name2, 1535);

        //create collection and store entries
        DataCollection dc = new DataCollection();
        dc.addEntry(turtle1);
        dc.addEntry(turtle2);

        //retrieve an entry
        System.out.println(dc.getEntry(name1));
    }
}

class DataCollection{

    HashMap<String, DataEntry> list;

    DataCollection(){ list = new HashMap<>();   }

    void addEntry(DataEntry entry) {
        //todo verify input
        list.put(entry.getName(), entry);
    }

    DataEntry getEntry(String name) {
       //todo verify input          
       return list.get(name);
    }
    //todo add more methods as needed such as remove, get all entries
}
Sign up to request clarification or add additional context in comments.

Comments

0

Keys are used with Maps.

Try replacing your ArrayList with a java.util.HashMap.

HashMap<String,DataBase> turtles = new HashMap<>();
turtles.put("Diamondback terrapin", new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years"));
...
DataBase database = turtles.get("Diamondback terrapin");

10 Comments

DataBase database = turtles.put("Diamondback terrapin"); ^ method Map.put(String,DataBase) is not applicable (actual and formal argument lists differ in length) method AbstractMap.put(String,DataBase) is not applicable (actual and formal argument lists differ in length) method HashMap.put(String,DataBase) is not applicable (actual and formal argument lists differ in length) 1 error is my result
I should have written turtles.get...the code is now updated.
It runs but not what I wanted. The code is supposed to allow the user to enter a name and then the results pop up kind of like a mini google
When you call turtles.get(key), it will return the corresponding database, and you can display the information from there.
@Keara when I run it, it just prints the information. I'm trying to make it so that when the user enters a name, the information relating to the name will show up.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.