This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HELP!" Watch "HELP!" New topic
Author

HELP!

Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
I am desperately trying to get a get method working, and I need it done by tonight and I have been battling for hours. I thought I had it done, but now the compiler says illegal start o expression (public). Why is this?

/**getMake method
* returns the make from a registration number
* @return the make
*/
public String getMake(String reg){
//Declare a return method
String makeReturn;
MakeSearch = SimpleIO.getString ("Enter registration number");
carIterator = carList.iterator();
while (carIterator.hasNext()){
Car resultCar = carIterator.next();

//Compare reg numbers
String result = resultCar.getReg();
if MakeSearch.equals(result){
makeReturn = resultCar.getMake();
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That error usually means there's an extra close-brace someplace before the line the error is reported on.

Lari, please keep these all in one thread, OK, so we can have an ongoing conversation!


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That error usually points to some missing or extra punctuation, often a line or two or three above the message. The if clause above the error needs parens around the boolean expression:

if ( MakeSearch.equals(result) ) ...

Keep chuggin away!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
I have checked that my {s are all closed above and I can't see any extra ones... at the risk of sending a massively long message I shall paste in my code, maybe any obvious errors could be pointed out? The problem public expression is in the ending 20 or so lines. Any help would be a massive relief. Thank-you

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The method right before getMake() is main(). By my count, there are two missing close braces at the end: one to close the "while (moreToRead)" loop, and one to close the method itself.

What are the three little "while" loops at the end of main() supposed to do, anyway? They each just count through an iterator without actually doing anything.
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
Well I had just found the missing }s as you replied - thank-you for getting back to me. However now I have found that the compiler is saying that it can't find carIterator (which is in the car Iterator above the getmake method). The confusion continues
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
As regards the three while loops in the iterators, I am new to ArrayLists and I put the Iterators in to read through the ArrayLists with the hope that the getMake method below will be able to use them to find a particular registration number
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Variables declared inside a method are local to that method; they're not visible to code in other methods. carIterator is declared in main(), so isn't visible in getMake(). The best solution is to declare another carIterator inside getMake(); there's no good reason to share this variable. Two methods can each have a local variable by the same name; they're unrelated.
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
BIG HUGE THANK-YOU FOR THAT. I have one error that has occured now I have remedied the last one...

1 error found:
File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\Vehicles.java [line: 192]
Error: variable makeReturn might not have been initialized

for the end of the code... the last few lines are below. If I can get this to work I will sleep happy (which has been looking rather unlikely!)

Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
Well, I still have no idea on this one so I think I may have to give up and get a drink... if anyone can shed some light on the situation for me you could make my day and maybe my compiler will be happy too!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lari Hopkins:
...Error: variable makeReturn might not have been initialized...

Unlike member variables, local variables are not automatically initialized to a default value, so it's up to the programmer to assign a value before use. The compiler is telling you that the local variable "makeReturn" might not have been initialized. (In particular, if the while loop doesn't execute...)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
clive jordan
Ranch Hand

Joined: Apr 27, 2005
Posts: 39
Hi Lari,

I've not really had time to look through your code but the the 'may not have been initialized' error usually occurs when you use a local varialble that has not been initialized first. (Instance variables are automatically initialized).

So to fix it do this when you declare the variable:

String makeReturn = null;

And that should keep the compiler happy.

Cheers,
Clive
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
Well I thought about declaring 'String makeReturn;' (although I'm not sure if that would be right) however adding that as a variable did not create a compiler error, but did nothing about the 'might not have been initialized' error msg. As a Java beginner I'm not sure I understand what I am to do in order to initialise (I have never encountered this msg before) is it a case of some variable declaration?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It's not just a matter of making the compiler shut up. The compiler produces that error message because there's an error in your code. You do this:which says that if the input string is equal to something or other, then you're going to return the value of the getMake() method. So far, so good. But what if the input string isn't equal to the something or other? What are you going to return in that case?If you answer that question, you will have assigned a value to makeReturn no matter what happens. This is a good thing for your program, and the compiler will have nothing to complain about.
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
I have attempted to assign a SimpleIO message in the case of a reg not being found....



But the same error message remains, I must be misunderstanding the problem.
clive jordan
Ranch Hand

Joined: Apr 27, 2005
Posts: 39
Hi Lari,

Yes, this makes your code more robust. However, a *local* variable must be initialized explicitly, when it is first declared.

Here you are declaring result and initializing at the same time. This is fine:
String result = resultCar.getReg();

In the following you are assigning a value to makeReturn, but in this case you need to declare and initialize makeReturn before you use it:
if (MakeSearch.equals(result)){
makeReturn = resultCar.getMake();
}
..

So first declare and initialize the variable:
String makeReturn = null; <--- this is where the money is !
Then use it:
if (MakeSearch.equals(result)){
makeReturn = resultCar.getMake();
}
..
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
That is a massive help. I wonder if I could get one last thing fixed before I get some sleep..

ColourSearch and ModelSearch are coming up with 'cannot find symbol' errors, however MakeSearch seems fine. Any ideas why?

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lari Hopkins:
...ColourSearch and ModelSearch are coming up with 'cannot find symbol' errors, however MakeSearch seems fine. Any ideas why? ...

It could be these were not properly declared as variables. (Remember, when declaring variables in Java, a type must be specified along with the name.) Or if they were declared, those declarations are not in the scope of where the variables are being used.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11357
    
  16

Lari,

I'm going to give you some unsolicited advice for your next project. I can't say for sure, but from all your posts and all your threads, you appear to have done this:

1) write your entire program
2) compile it
3) freak out when you have many many problems.

this is a BAD methodology. Don't feel bad - probably everone here did the exact same thing when they first started writing code. Here's a problem you may not have addressed yet - you spent so much time getting the code to compile, but are you sure it WORKS? I don't mean runs, I mean does what it is supposed to to in every case? That is often the harder part.

What most people who have done this for a while reccomend is this:

Assuming you have planned out what you're gonna do (i.e. you've THOUGHT about the problem, sketched out some pictures, written down in ENGLISH how your code is gonna go)


That second line is the critical one. Some would argue you should ONLY write/change one line of code before re-compiling. that way, if you start getting new compiler errors, you know where to focus.

But the mroe important idea here is that you don't write 100 lines, THEN start the process. As you hopefully have learned, you are then in comiler error hell. even once you get those fixed, you're still not sure your code works.

So when you decide to first implement a new method, write it with no body, or maybe nothing more than a print statement. Hardcode the return value to always be something. Once that works, have the method print the variables you pass in, to be sure they are working.

For some of your methods, like the getModel, add in the asking for registration number, and echo that back to the user. compile, test, debug, etc.

the LESS code you write between each compile/correct/test/debug iteration, the simpler your life will be.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Lari Hopkins
Ranch Hand

Joined: Nov 23, 2006
Posts: 37
I still cannot work out why the compiler can't find ModelSearch and ColourSearch when it doesn't have a problem with MakeSearch. If it is a matter of declaring somewhere, how do you declare a search?!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HELP!