• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i wrote a program using a recursive method and compiled it on jcreator, then...?!

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this code on jcreator using a recursive method and when i compiled it , it gave me this error: <identifier> expected even though i identified the two lists in the main method and i dont know what is the problem with it , could you please tell me what is the problem with my program?!!

thank you!!



So, this is my program ( i used Jcreator to compile it!)

this is the task that i hopefully did below in my program im supposed to : Write a recursive method that takes two sorted lists in increasing order.The method creates
and returns a new list representing the union of the two lists.The resulting list should be
sorted.
The method should traverse the lists only once.
The method should be implemented using isEmpty(), insertFirst(), insertLast(), deleteFirst(),
deleteLast(), getFirst(), getLast().
Do not use refrence manupilation. Implement it recursively.
Example:
list1 contains{1,2,4}, list2 contains {2,3,4,5} the output list should look like {1,2,3,4,5}.



 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your static union method doesn't have a return type. I am, of course, assuming that it is a method. If it is a constructor, then constructors can't be static.

Henry
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this line



Will give an error something like that, because there are a couple of missing things. A method declaration has to include the return type, and the argument list has to include the types of the arguments.

As far as the return type goes, your method has return statements that return a LinkList, and others that return nothing. You can only return nothing from a method that declares a "void" return type, and you can only return a LinkList from a method that declares it returns LinkList or a superclass of LinkList. I guess "LinkList" is the right return type, but some of your return statements will have to change.

As far as the arguments go, that's easy: the method takes two LinkLists as arguments, so we need to declare that. All together, this line would look like



Another problem I see right away is at the end of the method:


You're going to get an error that says the last line is "unreachable", which means that after the return, the method will have come to an end, and the last line can never be executed. You might want to replace both lines with something like

 
Nadine Ernest
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i changed my program a bit:

i changed the line of the method:



in the above line in the code:

i said the return type is Linklist

and i also declared the list1 and list2 as of type Linklist

and in the end instead of


i changed it to the following:




ans now the errors are:

cannot find symbol class Linklist>>>>>to solve this problem i will open the Linklist.class and the problem will be gone hopefully

now, with the second problem:


the error is : missing return value
why does it tell me that? all i want is for the method to stop. what to do?!!

thanks for the help!?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method can't just stop. Remember that when your method runs, somewhere, someone has called



and when you say "return something;", it's that "something" that gets assigned to "myList." If you really, truly wanted to return nothing, you could use

return null;

which returns a sort of "missing object", an empty value. But that isn't what you want here: you always want to return a list, although sometimes you might want to return an empty list. I'm not saying this is correct -- I'm not actually trying to help correct your recursive algorithm yet -- but you could replace the "return" with



to return an empty list at that point.
 
Nadine Ernest
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello ernest,

actually no, um, i dont want my method to return me an empty list!

i want it when it finds two empty lists it stops it doesnt enter my while loop like break;

so it is return null;coz both the lists are empty!!
 
Nadine Ernest
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,
i have another question concerning the same topic, what is the java code for the insertLast() method for the linked lists!?!

thanks all!

special thanks to ernest friedmann hill!
thank youu!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Returning null for an empty result is considered very bad practice. It leads to more complex code with more errors. Think about it: if you do that, then any code that uses "union" -- including the recursive calls -- now has to check for null before using the returned list. If it does not check, you'll end up with a NullPointerException. If, instead, you return an empty list, then the empty-input condition is just handled naturally. Much simpler, much nicer.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic