File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes variable counter might not have been initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable counter might not have been initialized" Watch "variable counter might not have been initialized" New topic
Author

variable counter might not have been initialized

Linda Selfridge
Greenhorn

Joined: Mar 26, 2011
Posts: 16
Hi all, after yesterday I finally managed to fix all of my errors but now only have one remaining - I knew about it to start with but cannot figure out a way to initalise it.

int.java:31: variable counter might not have been initialized
moves = ++counter; //count of all executed moves
^
1 error

Basically I need to have the counter, count the number of times the loop is executed in my program. Below is the code...



I am new at this and muddling my through an external course with very little support only lecture notes and text. But I am a hands on learner so need to see things in practise to understand and this has got me stumped.

I need the counter to work with the loop.

Thanks guys!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Linda Selfridge wrote:...I knew about it to start with but cannot figure out a way to initalise it...

You initialize it by assigning it some initial value. Note that class and instance variables receive default initialization values, but local variables (for example, those declared within a method) do not. That's why you need to assign an initial value to counter before it's used. So... What would be a logical starting value?

Also, is there a reason that counter and moves are floating-point doubles? Seems like ints might be more appropriate here.


"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
Linda Selfridge
Greenhorn

Joined: Mar 26, 2011
Posts: 16
Okay so I have fixed it and the program compiles but I now get .....

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:857)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at Point.main(Point.java:31)

When I input anything into the program.

Here is the updated code...


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Linda Selfridge wrote:Okay so I have fixed it and the program compiles but I now get .....

Exception in thread "main" java.util.InputMismatchException
at ...
at Point.main(Point.java:31)...

This is telling you that there is an InputMismatchException at line 31 of your code.

Line 31 is...

And according to the API documentation, an InputMismatchException is "Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type."

So to correct this, ask yourself: What is the expected type of nextInt()? And what are you inputting here?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Linda Selfridge wrote:

Also, I don't think this will work the way you're expecting. The above code says if "direction" is 1, then set "l" equal to x-1. If "direction" is 2, then set "L" equal to x-1. And so on.

Also, without break statements, all cases below the match will also execute. For example, if "direction" is 6, then it will set "D" equal to y-1, and set "u" equal to y+1, and set "U" equal to y+1...
Linda Selfridge
Greenhorn

Joined: Mar 26, 2011
Posts: 16
Marc

With the switch does that mean I need to put a break between each case and change the numbers to what the input options are?

And with the
What is the expected type of nextInt()? And what are you inputting here?
- I am really not getting this and don't understand.

The input from the user should be the letters that represent left, right, up or down.
Linda Selfridge
Greenhorn

Joined: Mar 26, 2011
Posts: 16
Right I have corrected the int issues but now....

I have an issue with the switch (see below error message)

Point.java:34: incompatible types
found : java.lang.Double
required: int
switch (direction) //compute (x,y) coordinates using switch method
^
1 error (the arrow is mean to be on the first letter of direction)

Which is why I changed it to int in the first place ..... is there something else I can use? Or am I going about this all wrong?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Linda Selfridge wrote:

Your variables have really bad, single-letter names. Use descriptive names. And why do you have two of each?

Linda Selfridge wrote:With the switch does that mean I need to put a break between each case and change the numbers to what the input options are?

Yes, you must include a break; statement after each case.

Linda Selfridge wrote:I have an issue with the switch (see below error message)

Point.java:34: incompatible types
found : java.lang.Double
required: int
switch (direction) //compute (x,y) coordinates using switch method
^
1 error (the arrow is mean to be on the first letter of direction)

You cannot use a Double in a switch statement. Why did you change direction to be a Double instead of an int?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think you're on the right track conceptually, but you're just having trouble connecting the pieces.

Could you post your current code?

The variable you're using for switch must be convertible (without being narrowed) to type int. That means it must be a char, byte, short, int (or a wrapper instance of these), or an enum. The error message says you're trying to use a Double (a wrapper instance of a double value), which will not work because it would need to be narrowed to work as an int.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable counter might not have been initialized