This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes i wrote a program using a recursive method and compiled it on jcreator, then...?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i wrote a program using a recursive method and compiled it on jcreator, then...?!" Watch "i wrote a program using a recursive method and compiled it on jcreator, then...?!" New topic
Author

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

Nadine Ernest
Greenhorn

Joined: Dec 10, 2008
Posts: 27
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}.





What Doesn't Kill You, Makes You Stronger!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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



[Jess in Action][AskingGoodQuestions]
Nadine Ernest
Greenhorn

Joined: Dec 10, 2008
Posts: 27
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Dec 10, 2008
Posts: 27
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

Joined: Dec 10, 2008
Posts: 27
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i wrote a program using a recursive method and compiled it on jcreator, then...?!