1

I'm trying to create a program where a pseudorandom number generator generates a number and then passes it to an object, which then calls different methods in other Java files. I'm getting errors such as"

Illegal start of type and expected at line 11 switch (randNumber) {

and orphaned case at line 12: case 1;

I've tried removing the space between case and 1, case and 2, and case and 3, however, that returns a orphaned default error.

Could anyone suggest what could be wrong? Both the random.drawHouse() and the random.drawSquare() statements call to two other source codes. I've yet to write the SC for random.drawCircle and I really want to fix everything else before that.

import java.util.Random; public class RandomFunTester { private RandomFunTester random; RandomFunTester random = new RandomFunTester(); int randNumber = random.nextInt(2) + 1; switch (randNumber) { case 1: random.drawHouse(); break; case 2: random.drawSquare(); break; case 3: random.drawCircles(); break; default: System.out.println("The random number generated is not between the values of 1 and 3."); } } 

1 Answer 1

4

Could anyone suggest what could be wrong?

You've put a switch statement in the middle of a class declaration. Statements like this must be in methods or constructors. You haven't given much context here, but you might want something like this:

class ShapeDrawer { private RandomFunTester random = new RandomFunTester(); void drawRandomShape() { int randNumber = random.nextInt(2) + 1; switch (randNumber) { case 1: random.drawHouse(); break; case 2: random.drawSquare(); break; case 3: random.drawCircles(); break; default: System.out.println("The random number generated is not " + "between the values of 1 and 3."); } } } 

Note that you'll never draw circles with this code, assuming RandomFunTester.nextInt() is similar to Random.nextInt() - I'd expect random.nextInt(2) to return either 0 or 1.

Additionally, RandomFunTester seems to have different roles - come up with a random number and drawing shapes. I realize this isn't production code, but it's never too early to think about giving each class a single responsibility.

Sign up to request clarification or add additional context in comments.

6 Comments

The object of this program was to generate a random number, I hope, through random.nextInt(2) + 1, and then pass it the value to the switch statement that will call method to draw various shapes. Not sure what is going here honestly. After implementing the switch into a method I see a ton of cannot find symbol errors...@JonSkeet
@user2744996: Well we don't know what the RandomFunTester class looks like, but it still sounds like it's mixing up different responsibilities. Do you actually need it to have a nextInt() method? Can't this class have a Random variable instead? Basically you haven't given us enough context to help you beyond what I've shown you, but hopefully that's enough to show you what was wrong with your original code. Within a class, you can declare variables, methods, constructors, nested types, and finalizers. Anything else - any "action" - should happen inside methods or constructors.
Truly sorry for not giving enough context. I think the thing is that it would need to generate a random number first through the nextInt method? What context would you need to correctly demonstrate the passing of the random number from the generator to the switch statement?
@user2744996: I'm saying that you don't need a different class to generate a random number, as java.util.Random already does that. Anyway, I suggest you use what's in my answer at the moment, and work from there - just make sure you put all the relevant statements in methods.
After implementing the switch statement into a method that looks pretty much like yours I get a slew of errors saying that the computer: Cannot Find Symbol: nextInt, and it doesn't know what I'm referring to in drawHouse(), drawSquare(), and drawCircles - but the other class files are all present(I've just written drawCircle. The method names are okay - its just that the computer isn't recognizing that I'm calling to the method.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.