-1

My aim is to create a database like code where i add planets and the year they are found, and once the user inputs planets it shows all the planets and the year they are found, so far i have created this.

import java.util.HashSet;
import java.util.Scanner;
import java.lang.*;
public class sky {
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    // HashSet declaration
    HashSet < String > planet =
      new HashSet < String > ();

    // Adding elements to the HashSet
    planet.add("planet A");
    planet.add("planet B");
    planet.add("planet C");
    planet.add("planet D");
    planet.add("planet E");
    //Addition of duplicate elements will not show
    planet.add("planet A");

    System.out.println("enter the word");
    String input = in .next();


    int count = 0;
    for (int i = 0; i < input.length(); i++)

    {
      if (sky.contains(input.HashSet(i))) count++;
    }

    System.out.println("the planets are");
    System.out.println(count);


    HashSet < String > date =
      new HashSet < String > ();

    // Adding elements to the HashSet
    date.add("2001");
    date.add("2002");
    date.add("2003");
    date.add("2004");
    date.add("2005");



    //Displaying HashSet elements
    System.out.println(planet);
    System.out.println(date);

  }
}

errors exsist

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Amr Mixy
  • 83
  • 1
  • 8
  • "errors exsist", well what are the errors? – phflack Dec 01 '15 at 16:39
  • ----jGRASP exec: javac -g sky.java sky.java:27: error: cannot find symbol { if(sky.contains(input.HashSet(i))) count++; } ^ symbol: method HashSet(int) location: variable input of type String 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. – Amr Mixy Dec 01 '15 at 16:41
  • 1
    Looks like you're trying to use `sky.contains(...)` when your class `sky` has no methods besides the main method, you'll need to call a real method if you expect it to do anything, the computer will not attempt to read your mind – phflack Dec 01 '15 at 16:41
  • what should i do then? – Amr Mixy Dec 01 '15 at 16:42
  • Write the method, or remove the call to the method – phflack Dec 01 '15 at 16:42
  • could you help me write a method to search for the planets? – Amr Mixy Dec 01 '15 at 16:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96703/discussion-between-amr-mixy-and-phflack). – Amr Mixy Dec 01 '15 at 16:44
  • check the chat again – phflack Dec 02 '15 at 02:39

2 Answers2

1

What you need is not a HashSet but a HashMap. Take a look at the program below and try to understand what exactly is going on here. I'll suggest you read how HashMap works before reading this.

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class sky {

  private static Map<String, Integer> planetMap = new HashMap<String,Integer>();
  public static void main(String args[]) {
    populateDB();
    Scanner scanner = new Scanner(System.in);

    String planetName = scanner.nextLine();

    if(planetMap.get(planetName) != null) {
      System.out.println("The planet "+ planetName +" was found in "+ planetMap.get(planetName));
    }
    else {
      System.out.println("Invalid Planet Name");
    }

  }

  public static void populateDB() {


    planetMap.put("Earth", 1600);
    planetMap.put("Mars", 1500);
    planetMap.put("Jupiter", 1100);
    planetMap.put("Saturn", 1900);
    planetMap.put("Venus", 1300);
  }
}
bluelurker
  • 1,353
  • 3
  • 19
  • 27
1

Similar to what @bluelurker had, but with the addition of a location field as you needed in chat

Using a custom Planet class instead of a String for the HashMap can let you store much more data about each Planet in an organized manner

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class sky
{
  private static Map<String, Planet> planetMap = new HashMap<String, Planet>();

  public static void main(String args[])
  {
    populateDB();
    Scanner scanner = new Scanner(System.in);

    String planetName = scanner.nextLine();
    Planet planet = planetMap.get(planetName);

    if(planet != null)
      System.out.println("The planet " + planet.name + " was found in " + planet.date + " and is currently located " + planet.location + ".");
    else
      System.out.println("Invalid Planet Name");
  }

  private static class Planet
  {
    public final String name;
    public final int date;
    public final String location;

    public Planet(String n, int d, String l)
    {
      name = n;
      date = d;
      location = l;
    }
  }

  public static void populateDB()
  {
    planetMap.put("Earth", new Planet("Earth", 1600, "here"));
    planetMap.put("Mars", new Planet("Mars", 1500, "near us"));
    planetMap.put("Jupiter", new Planet("Jupiter", 1100, "over there"));
    planetMap.put("Saturn", new Planet("Saturn", 1900, "above us"));
    planetMap.put("Venus", new Planet("Venus", 1300, "in the sky"));
  }
}
phflack
  • 2,729
  • 1
  • 9
  • 21