12 Replies - 1781 Views - Last Post: 06 May 2009 - 06:44 PM Rate Topic: -----

#1 natep67  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 22-October 08

Set up a Map

Posted 01 May 2009 - 04:37 PM

I cant figure out what i am doing! what do you have to import to use the map class? i have imported java.util.map and i still didn't compile correctly. and also how do you set up a the Info class as the value of a map

Map
import java.util.Map  
import java.util.Collection;
import java.util.Set;
	public interface Map {

		//Basic operations
		V put(K key, V value);
		V get(Object key);
		V remove(Object key);
		boolean containsKey(Object key);
		boolean containsValue(Object value);
		int size();
		boolean isEmpty();
		
		//Bulk operations
		void putAll(Map<String extends K,Info, extends V> t);
		void clear();

		// Collection Views
		public Set<K> keySet();
		public Collection<V> values();
		public Set<Map.Entry<K,V>> entrySet();
		public interface Entry {
			K getKey();
			V getValue();
			V setValue(V value);
		}
	}




Info
public class Info{
   //Attributes
   static String name;
   static int age;
  //Constructor
  public void Info(final String name, final int age){
	  Info.name = name;
	  Info.age = age;
  }
  {
	   name = People.aName; 
	   }
  {
	   age= People.numberCount;
	   }
 public String getName(){
	 return name;
 }

 public int getBDay(){
	 
	 return age;
 }

}

This post has been edited by natep67: 01 May 2009 - 04:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Set up a Map

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Set up a Map

Posted 01 May 2009 - 04:58 PM

Looks like your missing a semicolon at the end of your first import statement. ;)

This post has been edited by Locke: 01 May 2009 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 natep67  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 22-October 08

Re: Set up a Map

Posted 01 May 2009 - 05:08 PM

The import is correct now but it says that k,v,set and collection cannot be resolved to a type
import java.util.Collection;
import java.util.Map;
import java.util.Set;

	public interface Maps {

		//Basic operations
		V put(K key, V value);
		V get(Object key);
		V remove(Object key);
		boolean containsKey(Object key);
		boolean containsValue(Object value);
		int size();
		boolean isEmpty();
		
		//Bulk operations
		void putAll(Map<String extends K,Info, extends V> t);
		void clear();

		// Collection Views
		public Set<K> keySet();
		public Collection<V> values();
		public Set<Map.Entry<K,V>> entrySet();
		public interface Entry {
			K getKey();
			V getValue();
			V setValue(V value);
		}
	}



 

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Set up a Map

Posted 01 May 2009 - 05:15 PM

K and V aren't types, unless you made them. You probably want a <K, V> in your class declaration.

And Set is an interface, and can't have instantiations. The closest interfaces can get to instantiations are anonymous objects.

This post has been edited by Locke: 01 May 2009 - 05:16 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Set up a Map

Posted 01 May 2009 - 08:25 PM

and not a good idea to call your interface Map if you import java.util.Map
will be really confsuing to read your code...
Was This Post Helpful? 0
  • +
  • -

#6 natep67  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 22-October 08

Re: Set up a Map

Posted 04 May 2009 - 02:38 PM

I almost have this done but im running into a few problems especially on lines 23 and 24(the beganing of the if statement). i get this error on both items "Syntax error on token ">", Expression expected after this token". Please help me out!
  import java.util.HashMap;
import java.util.Scanner;
public class Info{
  private String name;
  private  int age;
  public Info(final String name, final int age){  
  
   this.name = name;
   this.age = age;
  }
	public interface Maps<K, V> {
		 HashMap<String, Info>m= new HashMap<String, Info>();
		 public class If {
		 		public void main(String[] args) {
					 Scanner input = new Scanner(System.in);
		 			if (input.hasNext()= Maps<K>) {
		 				System.out.println(Maps<K, V>);
		 			} else {
		 				System.out.println("Sorry, Name not entered. Try again");
		 			}
		 				

		 	}

		 }

}

} 

This post has been edited by natep67: 04 May 2009 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Set up a Map

Posted 04 May 2009 - 03:46 PM

I don't think you need anything else in the if statement besides input.hasNext().

And if you do need something else there...then that's certainly not the syntax for it.
Was This Post Helpful? 1
  • +
  • -

#8 natep67  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 22-October 08

Re: Set up a Map

Posted 04 May 2009 - 04:09 PM

i need this line to equal a name that was typed in earlier so it can pull up info on that name.Im trying to set up my code so when the user inputs a name and Birthday it assigns the name as the key and the birthday as the value of a map. So when the user inputs a name it will pull up the info attached to it.
Was This Post Helpful? 0
  • +
  • -

#9 natep67  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 22-October 08

Re: Set up a Map

Posted 05 May 2009 - 04:28 AM

I am doing a whole lot better this morning. i was working on this and i think i have solved it for what i want except for one problem. On line 16, i get all these errors.-
Syntax error on tokens, ConstructorHeaderName expected
instead
- Syntax error on token "(", delete this token
- Syntax error on token ")", delete this token
- Syntax error on token "(", < expected
- Watchpoint:Info [access and modification] - People
I realize a lot of it is syntax but i don't know what to do with the parentheses
New Code
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Info{
  private  int destinyNumber;
  private  int lifePathNumber;
  public Info(final int destinyNumber, final int lifePathNumber){  
  
   this.destinyNumber = destinyNumber;
   this.lifePathNumber = lifePathNumber;
  }
Map<String, Info> myMap = new HashMap<String, Info>();
//create an Info item to put into the map, keyed by its name
Info item = new Info(destinyNumber ,lifePathNumber);
 myMap.put (People.sum , People.numberCount));
		 public class If {
			 {
					 Scanner input = new Scanner(System.in);
		 			if (input.hasNext(People.aName)) {
		 				System.out.println(People.numberCount);
		 			} else {
		 				System.out.println("Sorry, Name not entered. Try again");
		 			}
		 				
 
		 	}
 
		 }
 
}
 
}


Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Set up a Map

Posted 05 May 2009 - 10:10 AM

I think it's the line myMap.put (People.sum , People.numberCount));

There's an extra ) there.
Was This Post Helpful? 0
  • +
  • -

#11 natep67  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 22-October 08

Re: Set up a Map

Posted 05 May 2009 - 05:04 PM

That doesnt fix anything
what did fix most of them though was replacing the ) with >im still getting these errors

Quote

Syntax error on token ">", VariableDeclarator expected after
this token
on the same lines

This post has been edited by natep67: 05 May 2009 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#12 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Set up a Map

Posted 06 May 2009 - 10:59 AM

View Postnatep67, on 5 May, 2009 - 06:28 AM, said:

I am doing a whole lot better this morning. i was working on this and i think i have solved it for what i want except for one problem. On line 16, i get all these errors.-
Syntax error on tokens, ConstructorHeaderName expected
instead
- Syntax error on token "(", delete this token
- Syntax error on token ")", delete this token
- Syntax error on token "(", < expected
- Watchpoint:Info [access and modification] - People
I realize a lot of it is syntax but i don't know what to do with the parentheses
New Code
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Info{
  private  int destinyNumber;
  private  int lifePathNumber;
  public Info(final int destinyNumber, final int lifePathNumber){  
  
   this.destinyNumber = destinyNumber;
   this.lifePathNumber = lifePathNumber;
  }
Map<String, Info> myMap = new HashMap<String, Info>();
//create an Info item to put into the map, keyed by its name
Info item = new Info(destinyNumber ,lifePathNumber);
 myMap.put (People.sum , People.numberCount));
		 public class If {
			 {
					 Scanner input = new Scanner(System.in);
		 			if (input.hasNext(People.aName)) {
		 				System.out.println(People.numberCount);
		 			} else {
		 				System.out.println("Sorry, Name not entered. Try again");
		 			}
		 				
 
		 	}
 
		 }
 
}
 
}


This code is rather strange. Are you declaring two classes in one file? and what is this "If" class? Also, all your code in a class must be in a method, unless it's a member variable etc.

but lines like
 myMap.put (People.sum , People.numberCount));

definitely need to be in a method rather than just floating around in your class.

-Michael
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Set up a Map

Posted 06 May 2009 - 06:44 PM

myMap.put (People.sum , People.numberCount));
is definitevely wrong
can be

myMap.put (People.sum , People.numberCount);
if numberCount is a variable

or
myMap.put (People.sum , People.numberCount());
is a method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1