This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am getting "void type not allowed here" error

 
Deepu Dangol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.ArrayList;

public class TestArrayList
{
public static void main(String[ ] args)
{
ArrayList list = new ArrayList( );
list.add("this");
list.add("is");
list.add("lots");
list.add("of");
list.add("fun");
list.add("for");
list.add("every");
list.add("java");
list.add("programmer");
System.out.println(list.toString());

}

public static void markLength4(ArrayList<String> list)
{
int index = 0;
while(index < list.size())
{
String next = (String)list.get(index);
if(next.length( ) == 4)
{
list.add(index, "****");
index = index + 1;
}
else
index = index + 1;
}

for(index = 0; index < list.size( ) ; index++)
{
System.out.println(list.get(index));

}
System.out.println(markLength4(list));
}
}
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Dmx Dangol':

You're trying to return a value from a method marked as void. You can't do that.

Also, just as a friendly suggestion, you need to change your username to your real name. That is one of the rules of this forum, and the moderators will enforce it.

John.
 
Deepu Dangol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What shoiuld I do then?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what do you think you should do?

John.
 
Deepu Dangol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the void to String and returned as return markLength4(list) but didn't get the result.
 
Campbell Ritchie
Sheriff
Posts: 48396
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method has a return type; it must return something which matches that type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic