incompatible types

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2673 Views - Last Post: 12 April 2007 - 10:50 AM Rate Topic: -----

#1 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

incompatible types

Posted 09 April 2007 - 10:21 AM

Hi There
Can somebody help me with this code


public static String getFullName(Character letter) {
String fullname = null;

String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};


Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<letters.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<letters.length; i++)
letter = m.get(letters[i]);
String fullname = m.get(fullnames[i]);
return fullname;
I got an error getFullName(java.lang.Character) in ProteinSequence cannot be applied to (java.lang.String)
System.out.println(seq.getFullName("G"));
^
I wonder why if I defined letters to be character array

This post has been edited by stanka: 09 April 2007 - 10:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: incompatible types

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 09 April 2007 - 11:08 AM

Looks like you're calling m.get with a String instead of a Character. Your key is a Character, no?
Was This Post Helpful? 0
  • +
  • -

#3 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 09 April 2007 - 11:29 AM

Hi Thanks
Now I got another error

ProteinSequence.java:60: incompatible types
found : java.lang.String
required: java.lang.Character
letter = m.get(letters[i]);
I wonder why if letter and letters[i] are characters??? Please help?

This is the code

public static String getFullName(Character letter) {
String fullname = null;

String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};


Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<letters.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<letters.length; i++)
letter = m.get(letters[i]);
String fullname = m.get(fullnames[i]);
return fullname;
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 09 April 2007 - 11:35 AM

I'm nost sure what you changed, but I still see this error:
String fullname = m.get(fullnames[i]);

This post has been edited by alcdotcom: 09 April 2007 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 09 April 2007 - 12:00 PM

View Postalcdotcom, on 9 Apr, 2007 - 11:35 AM, said:

I'm nost sure what you changed, but I still see this error:
String fullname = m.get(fullnames[i]);

Hi
I changed to:

public static String getFullName(Character letter) {
String fullname = null;
String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<letters.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<m.size(); i++){
letter = m.get(letters[i]);
fullname = m.get(fullnames[i]);}
return fullname;
}

I have got the same error: ProteinSequence.java:60: incompatible types
found : java.lang.String
required: java.lang.Character
letter = m.get(letters[i]);
^
Any help will be appriciated
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 09 April 2007 - 03:31 PM

View Poststanka, on 9 Apr, 2007 - 12:00 PM, said:

View Postalcdotcom, on 9 Apr, 2007 - 11:35 AM, said:

I'm nost sure what you changed, but I still see this error:
String fullname = m.get(fullnames[i]);

Hi
I changed to:

public static String getFullName(Character letter) {
String fullname = null;
String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<letters.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<m.size(); i++){
letter = m.get(letters[i]);
fullname = m.get(fullnames[i]);}
return fullname;
}

I have got the same error: ProteinSequence.java:60: incompatible types
found : java.lang.String
required: java.lang.Character
letter = m.get(letters[i]);
^
Any help will be appriciated


Look at the error output. It's telling you that "letter = m.get(letters[i]);" is your error. Think about it. map is a HashMap<Character, String>. The format is HashMap<Key, Value>. So when you call hashmap.get(k), the "k" is a key and it returns a value. so you pass get a Character and it returns a String. Now look at that line again and ask yourself, "Is letter a String or a Character? What is the return type of get? Do they match?"

This post has been edited by alcdotcom: 09 April 2007 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 10 April 2007 - 08:24 AM

Hi, alcdotcom
Thanks a lot. That was a great advise :)
BUT another problem came up.
I do not understand why I get only one value (the last one in the String[]fulnames each time I call the method in the main. For example the value for the key'V' or key 'G' is always the same- "stop".
Is there something wrong in the way I put the keys and the values in the map.
I hope somebody can help
This is the code again:

public static String getFullName(Character letter) {
String fullname = null;
String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<fullnames.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<m.size(); i++){
letter = letters[i];
fullname = m.get(letters[i]);}
return fullname;
Was This Post Helpful? 0
  • +
  • -

#8 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 10 April 2007 - 09:27 AM

View Poststanka, on 10 Apr, 2007 - 08:24 AM, said:

Hi, alcdotcom
Thanks a lot. That was a great advise :)
BUT another problem came up.
I do not understand why I get only one value (the last one in the String[]fulnames each time I call the method in the main. For example the value for the key'V' or key 'G' is always the same- "stop".
Is there something wrong in the way I put the keys and the values in the map.
I hope somebody can help
This is the code again:

public static String getFullName(Character letter) {
String fullname = null;
String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<fullnames.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<m.size(); i++){
letter = letters[i];
fullname = m.get(letters[i]);}
return fullname;


Ok, for starters, I think you'd be able to see the problem a lot more clearly if you made your code a little more readable. Also, when you're posting your code, put them in code tags. Look at my version:
public static String getFullName(Character letter) {
	String fullname = null;
	String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
	Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
	Map<Character,String> m = new HashMap<Character, String>();
	
	for (int i=0; i<fullnames.length; i++){ 
		m.put(letters[i], fullnames[i]);
	}
	
	for (int i=0; i<m.size(); i++){
		letter = letters[i];
		fullname = m.get(letters[i]);
	}
	return fullname;
}	


It's the same thing, only much more readable. Look at your second loop. It loops through both arrays, assigning letter a value and fullName a value during each iteration. Ask yourself what purpose does it serve? What can you change/remove that would make this method do what you want?
Was This Post Helpful? 0
  • +
  • -

#9 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 10 April 2007 - 11:31 AM

Hi
I am afarid that I need more hints?


View Postalcdotcom, on 10 Apr, 2007 - 09:27 AM, said:

View Poststanka, on 10 Apr, 2007 - 08:24 AM, said:

Hi, alcdotcom
Thanks a lot. That was a great advise :)
BUT another problem came up.
I do not understand why I get only one value (the last one in the String[]fulnames each time I call the method in the main. For example the value for the key'V' or key 'G' is always the same- "stop".
Is there something wrong in the way I put the keys and the values in the map.
I hope somebody can help
This is the code again:

public static String getFullName(Character letter) {
String fullname = null;
String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m = new HashMap<Character, String>();
for (int i=0; i<fullnames.length; i++){ m.put(letters[i], fullnames[i]); }
for (int i=0; i<m.size(); i++){
letter = letters[i];
fullname = m.get(letters[i]);}
return fullname;


Ok, for starters, I think you'd be able to see the problem a lot more clearly if you made your code a little more readable. Also, when you're posting your code, put them in code tags. Look at my version:
public static String getFullName(Character letter) {
	String fullname = null;
	String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
	Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
	Map<Character,String> m = new HashMap<Character, String>();
	
	for (int i=0; i<fullnames.length; i++){ 
		m.put(letters[i], fullnames[i]);
	}
	
	for (int i=0; i<m.size(); i++){
		letter = letters[i];
		fullname = m.get(letters[i]);
	}
	return fullname;
}	


It's the same thing, only much more readable. Look at your second loop. It loops through both arrays, assigning letter a value and fullName a value during each iteration. Ask yourself what purpose does it serve? What can you change/remove that would make this method do what you want?

Quote

 
 

Was This Post Helpful? 0
  • +
  • -

#10 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 10 April 2007 - 12:47 PM

OK, consider this (your code):

for (int i=0; i<m.size(); i++){
		letter = letters[i];  // <---this is redundant
		fullname = m.get(letters[i]);
	}
	return fullname;


This loop executes each time the method is called. Let's walk through a couple of iterations.

1. when i == 0
letters[0] == 'G'
fullName = m.get('G') == "Glycine"

2. when i == 1
letters[1] == 'A'
fullName = m.get('A') == "Alanine"

...
... skipping a few iterations...
...

3. when i == m.size() - 1
letters[1] == '*'
fullName = m.get('A') == "stop"

So the loop continues until the loop gets to the end of the array and then your method returns fullName. You get it now?
Was This Post Helpful? 0
  • +
  • -

#11 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 10 April 2007 - 03:12 PM

View Postalcdotcom, on 10 Apr, 2007 - 12:47 PM, said:

OK, consider this (your code):

for (int i=0; i<m.size(); i++){
		letter = letters[i];  // <---this is redundant
		fullname = m.get(letters[i]);
	}
	return fullname;


This loop executes each time the method is called. Let's walk through a couple of iterations.

1. when i == 0
letters[0] == 'G'
fullName = m.get('G') == "Glycine"

2. when i == 1
letters[1] == 'A'
fullName = m.get('A') == "Alanine"

...
... skipping a few iterations...
...

3. when i == m.size() - 1
letters[1] == '*'
fullName = m.get('A') == "stop"

So the loop continues until the loop gets to the end of the array and then your method returns fullName. You get it now?


Thanks,
I see. How, then I can assign a value to letter, which is an element from the Char[]letters array. If I call m.getKey(i) I have got an error : Cannot find symbol
cannot find symbol
symbol : method getKey()
location: interface java.util.Map<java.lang.Character,java.lang.String>
{letter = m.getKey();

BTW How I can put my code into a box.


This is the code:

public static String getFullName(Character letter) {
String fullname = null;
String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
Map<Character,String> m  = new HashMap<Character, String>();
 for (int i=0; i<fullnames.length; i++){ m.put(letters[i], fullnames[i]); }
 letter = m.getKey();
  for (int i=0; i<m.size(); i++)
fullname = m.get(letters[i]);
return fullname;
}

This post has been edited by alcdotcom: 11 April 2007 - 03:42 AM

Was This Post Helpful? 0
  • +
  • -

#12 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 11 April 2007 - 04:16 AM

I added the code box for you. The way you do it is, drag your mouse to select/highlight all of your code and then click the button with the "#." You should read the BB Code help page. Click the link that says "BB Code Help" below the smileys.

Ok. I'm going to give you some advice on this method. It can be really really simple. First, you create two arrays every time the method is called. Since these arrays don't change then it would be better to declare and create them in your class constructor or in the beginning of your class and make them final.

public class YourClass {

   private final String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
   private final Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
...


Now that these are declared outside of your method they are only declared and created once, instead of every time the method is called. You can also access them from within the method because they have global scope.

Next, consider the loop that fills your map. That happens every time the method is called too. Since it's the same every time, why not put it in the constructor? So, now we have:

public class YourClass {

   private final String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
   private final Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
   private Map<Character,String> m;

   public YourClass() {  // constructor
	  m  = new HashMap<Character, String>();
	  for (int i=0; i<fullnames.length; i++) {
		 m.put(letters[i], fullnames[i]);
	  }
   }
...

Above I have declared the map, "m", with the arrays, so it also has global scope. In the constructor, I've initialized m and also filled it with the data from the arrays. notice the indentation I've used for the loop. Isn't that easier to read than putting it on one line? So, after breaking all of this unnecessary stuff out of your getFullName method it leaves this:

public static String getFullName(Character letter) {
   String fullname = null;
   letter = m.getKey();
   for (int i=0; i<m.size(); i++)
	  fullname = m.get(letters[i]);
   return fullname;
}


I want to help you with this, but I also want you to figure it out for yourself. So, think about this. You have a Character as an input and you need a String as an output. You have a map which takes a Character as an input and returns a String as an output. If I wanted to know the String value of Character key 'G' in m, I would call m.get('G');. That's it. Now, look at your method. It's doing too much and it's also doing the wrong things. I recommend thinking about what I've said and then starting over with that method. Just delete everything in the method and then think about how you can use the functionality that is built in to the Map to make this method return the right value. You might even think about the method as a wrapper for your Map, if that makes sense.
Was This Post Helpful? 0
  • +
  • -

#13 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 11 April 2007 - 08:25 AM

Hi
Thanks a lot for your help.

I removed the second loop and changed the code from :

for (int i=0; i<m.size(); i++)
fullname = m.get(letters[i]);

to:

fullnames= map.get(letters);

I am still having trobles to understand how to define the parameter letter in the code.
I read a lot of literature on maps but... I feel completely stuck.
Please help!



View Postalcdotcom, on 11 Apr, 2007 - 04:16 AM, said:

I added the code box for you. The way you do it is, drag your mouse to select/highlight all of your code and then click the button with the "#." You should read the BB Code help page. Click the link that says "BB Code Help" below the smileys.

Ok. I'm going to give you some advice on this method. It can be really really simple. First, you create two arrays every time the method is called. Since these arrays don't change then it would be better to declare and create them in your class constructor or in the beginning of your class and make them final.

public class YourClass {

   private final String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
   private final Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
...


Now that these are declared outside of your method they are only declared and created once, instead of every time the method is called. You can also access them from within the method because they have global scope.

Next, consider the loop that fills your map. That happens every time the method is called too. Since it's the same every time, why not put it in the constructor? So, now we have:

public class YourClass {

   private final String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
   private final Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
   private Map<Character,String> m;

   public YourClass() {  // constructor
	  m  = new HashMap<Character, String>();
	  for (int i=0; i<fullnames.length; i++) {
		 m.put(letters[i], fullnames[i]);
	  }
   }
...

Above I have declared the map, "m", with the arrays, so it also has global scope. In the constructor, I've initialized m and also filled it with the data from the arrays. notice the indentation I've used for the loop. Isn't that easier to read than putting it on one line? So, after breaking all of this unnecessary stuff out of your getFullName method it leaves this:

public static String getFullName(Character letter) {
   String fullname = null;
   letter = m.getKey();
   for (int i=0; i<m.size(); i++)
	  fullname = m.get(letters[i]);
   return fullname;
}


I want to help you with this, but I also want you to figure it out for yourself. So, think about this. You have a Character as an input and you need a String as an output. You have a map which takes a Character as an input and returns a String as an output. If I wanted to know the String value of Character key 'G' in m, I would call m.get('G');. That's it. Now, look at your method. It's doing too much and it's also doing the wrong things. I recommend thinking about what I've said and then starting over with that method. Just delete everything in the method and then think about how you can use the functionality that is built in to the Map to make this method return the right value. You might even think about the method as a wrapper for your Map, if that makes sense.

This post has been edited by stanka: 11 April 2007 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

#14 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 11 April 2007 - 09:36 AM

I'm not entirely sure what you're saying. Could you post your current method?
Was This Post Helpful? 0
  • +
  • -

#15 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 11 April 2007 - 12:13 PM

Thanks for helping me.
I can not think of anything else then to change code to:

public static String getFullName(Character letter) {
   String fullname = null;
   letter = m.getKey();
   for (int i=0; i<m.size(); i++)
	  fullname = m.get(letter);
   return fullname;


:blink:

I still do not understand how to define the variable letter in the method. How to retrieve the key from the map.




View Postalcdotcom, on 11 Apr, 2007 - 04:16 AM, said:

I added the code box for you. The way you do it is, drag your mouse to select/highlight all of your code and then click the button with the "#." You should read the BB Code help page. Click the link that says "BB Code Help" below the smileys.

Ok. I'm going to give you some advice on this method. It can be really really simple. First, you create two arrays every time the method is called. Since these arrays don't change then it would be better to declare and create them in your class constructor or in the beginning of your class and make them final.

public class YourClass {

   private final String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
   private final Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
...


Now that these are declared outside of your method they are only declared and created once, instead of every time the method is called. You can also access them from within the method because they have global scope.

Next, consider the loop that fills your map. That happens every time the method is called too. Since it's the same every time, why not put it in the constructor? So, now we have:

public class YourClass {

   private final String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};
   private final Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};
   private Map<Character,String> m;

   public YourClass() {  // constructor
	  m  = new HashMap<Character, String>();
	  for (int i=0; i<fullnames.length; i++) {
		 m.put(letters[i], fullnames[i]);
	  }
   }
...

Above I have declared the map, "m", with the arrays, so it also has global scope. In the constructor, I've initialized m and also filled it with the data from the arrays. notice the indentation I've used for the loop. Isn't that easier to read than putting it on one line? So, after breaking all of this unnecessary stuff out of your getFullName method it leaves this:

public static String getFullName(Character letter) {
   String fullname = null;
   letter = m.getKey();
   for (int i=0; i<m.size(); i++)
	  fullname = m.get(letters[i]);
   return fullname;
}


I want to help you with this, but I also want you to figure it out for yourself. So, think about this. You have a Character as an input and you need a String as an output. You have a map which takes a Character as an input and returns a String as an output. If I wanted to know the String value of Character key 'G' in m, I would call m.get('G');. That's it. Now, look at your method. It's doing too much and it's also doing the wrong things. I recommend thinking about what I've said and then starting over with that method. Just delete everything in the method and then think about how you can use the functionality that is built in to the Map to make this method return the right value. You might even think about the method as a wrapper for your Map, if that makes sense.

This post has been edited by alcdotcom: 11 April 2007 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2