This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

I am getting "void type not allowed here" error

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
}
}
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What shoiuld I do then?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the void to String and returned as return markLength4(list) but didn't get the result.
 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method has a return type; it must return something which matches that type.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic