-
Beginner for Java, need help with this code I wrote
Hey,
I'm new to Java and this forum. I have been reading the online book JavaNotes to learn Java, and I wrote this code. I couldn't figure out what was wrong with it using the book, except for that I knew I was probably using the wrong types (int, double, char, etc). I'm also pretty sure that Math.rnd(1-4) is not a correct command.
Will someone please help and expain? It would also be great if you could tell me how to get input that is text instead of numbers.
P.S: I am using a file called TextIO for my input. You can find it at the JavaNotes page.
Here is the code I wrote:
/* A program that chooses a suit and makes you guess it. */
public class Suits {
enum Suits{CLUB, SPADE, DIAMOND, HEART};
public static void main(String[] arg) {
double userInp;
int st;
userInp=TextIO.getlnInt;
st=Math.rndDouble();
System.out.print("I'm thinking of a suit. Can you guess it?");
while (userInp=st) {
System.out.print("You got it right!");
}
System.out.print("You got it wrong.");
} //end of main
} //end of class Suits
-
Re: Beginner for Java, need help with this code I wrote
Can you explain your problem.
If you are getting errors, copy and paste them here.
If the output is wrong, copy and paste it here and explain what is wrong with it.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
-
Re: Beginner for Java, need help with this code I wrote
Sorry, Norm. Thank you form reminding me.
Disregard the part about input, I have learned how to use a Scanner now. The errors that I am getting are things like "cannot convert to double" or something like that. It also does not recognise the Math.random, however I may have figured that out too. I just need some help on the Math.random now. If I am not mistaken, you can convert it with a code such as
(int) (Math.random());
? And if I wanted to choose a number between x and x, then I would do
(int) (Math.random()*x) + x;
?
Thanks.
-
Re: Beginner for Java, need help with this code I wrote
Have you written a small test program to see what the compiler thinks of your code?
If it compiles then execute it and see what it does.
When you get errors you don't understand
or have code that doesn't do what you want,
post the code and the errors and/or questions.
What you have posted won't begin to compile. For testing you need to post all of the code.
-
Re: Beginner for Java, need help with this code I wrote
Quote:
? And if I wanted to choose a number between x and x, then I would do
(int) (Math.random()*x) + x;
The formula for getting a random number between two integers, let's say a and b, is to multiply Math.random() by the positive difference of a and b, then add the lower of the two.
Quote:
while (userInp=st)
You're using an assignment operator here; in other words, you're telling the program to set the value of userInp to the value of st. To TEST if those two values are the same, you must use TWO equals signs.
while(userInp==st)
-
Re: Beginner for Java, need help with this code I wrote
OK, new problem with same (or similair) code.
I re wrote the code in my previous post with the java.util.Scanner, and here is what it looks like:
import java.util.Scanner;
public class Cards {
enum Suits { Club, Diamond, Spade, Heart }
public static void main(String[] args) {
String userInp;
String realSuit;
Scanner scan=new Scanner(System.in);
System.out.print("I'm thinking of a suit. Can you guess what it is?");
userInp=scan.nextLine();
switch ( (double) 3*Math.random() ) {
case 1:
realSuit="Club";
break;
case 2:
realSuit="Diamond";
break;
case 3:
realSuit="Diamond";
break;
case default:
realSuit="Heart";
break;
} //end of switch
if (userInp=realSuit) {
System.out.print("You got it right!");
}
else {
System.out.print("You got it wrong.");
} //end of if statement
}
}
I attempted to use the switch statement to choose the random variable for the suit.
Here is the error code I get when I run it in Eclipse:
Error: Main method not found in class Cards$Suits, please define the main method as:
public static void main(String[] args)
Also, in Eclipse, the word "default" is underlined in red and the error code says:
Syntax error on token "default", invalid ConstantExpression
I was following JavaNotes again, and I am unsure as to why this doesn't work.
@Norm When I write a program, it doesn't not do what it's supposed to do because they never compile. So I just need to check my errors on the code so it can actually compile, THEN I'll test for what I want it to do.
-Silent
EDIT::::::::::::::::::::::::::::::::::::::
For anyone who is wondering, the program is supposed to randomly choose a suit and make you guess it. If you guess correctly, it says you got it right, and vice versa.
EDIT:::::::::::::::::::::::::::::::::::::
@snowguy OMG thank you SO MUCH! I completely forgot that from the book I was reading. I will test that and get back to you.
EDIT::::::::::::::::::::::::::::::::::::::
@snowguy The == sign did not make any difference to the error code entioned earlier in the console, but it might save me from future errors. Thanks again.
-
Re: Beginner for Java, need help with this code I wrote
Please edit your post and add cod tags. See:
BB Code List - Java Programming Forums - The Java Community
Quote:
"default", invalid ConstantExpression
You need to read the doc for how to use a switch statement and how to code default.
It goes by itself.
You need to put the main() method it the class that will be the class that you will execute first.
Quote:
because they never compile.
The compiler prints out error messages describing the errors that it finds. For example:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
The above is what I was asking for when I asked for you to post the error messages.
The message includes the source line and the source line number(138 here). Very useful for finding the problem.
-
Re: Beginner for Java, need help with this code I wrote
Alright......
When I was reading the doc, the sample problem was this:
String computerMove;
switch ( (int)(3*Math.random()) ) {
case 0:
computerMove = "Rock";
break;
case 1:
computerMove = "Scissors";
break;
case 2:
computerMove = "Paper";
break;
}
System.out.println("Computer's move is " + computerMove);
But it said it had an error, because the computer thought it possible to not have computerMove assigned a variable. So in order to solve this problem, the doc said, and I quote:
"The easiest way to fix the problem is to replace the case label case 2 with default. The computer can then see that a value is assigned to computerMove in all cases."
Apparently, this didn't work on my code, as I told you before, when I ran the java file in the cmd and in java the errors were the same:
Syntax error on token "default", invalid ConstantExpression
Also, when I tried to run the class file on the cmd, it also said, as well as in Eclipse:
Error: Main method not found in class Cards$Suits, please define the main method as:
public static void main(String[] args)
In my class, the public static void main stuff is already there, so I'm not sure what the error means. Some elaboration would help.
-Silent
-
Re: Beginner for Java, need help with this code I wrote
Quote:
possible to not have computerMove assigned a variable
Yes. There is no default clause. The compiler doesn't know what the switch() value will be.
You need to read the tutorial about how to write and execute a java program:
Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
Trail: Learning the Java Language (The Java™ Tutorials)
-
Re: Beginner for Java, need help with this code I wrote
Ummm.......
Why? I already know how to write AND execute a program. I have written quite a few, even. That work.
If you could elaborate on what SPECIFICALLY the computer means by
Error: Main method not found in class Cards$Suits, please define the main method as:
public static void main(String[] args)
Then that would be helpful.
If you need elaboration, then look at the code. From what I am reading, the error says that I don't have the oublic static void main(String[] arg) { start for my class. However, I do. Do I need to modify the start in some way?
-
Re: Beginner for Java, need help with this code I wrote
Quote:
Main method not found in class Cards$Suits,
That means: The M(???)/main method was not found in the class Cards$Suits.
It seems pretty clear to me.
What command are you executing that gets that error message?
What class file was involved?
What is the contents of that class file? Can you post the source?
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
-
Re: Beginner for Java, need help with this code I wrote
Alright, here:
Code java:
public class Cards {
enum Suits { Club, Diamond, Spade, Heart }
public static void main(String[] args) {
String userInp;
String realSuit;
Scanner scan=new Scanner(System.in);
System.out.print("I'm thinking of a suit. Can you guess what it is?");
userInp=scan.nextLine();
switch ( (double) 3*Math.random() ) {
case 1:
realSuit="Club";
break;
case 2:
realSuit="Diamond";
break;
case 3:
realSuit="Diamond";
break;
case default:
realSuit="Heart";
break;
} //end of switch
if (userInp==realSuit) {
System.out.print("You got it right!");
}
else {
System.out.print("You got it wrong.");
} //end of if statement
}
}
-
Re: Beginner for Java, need help with this code I wrote
You forget to answer these two questions:
What command are you executing that gets that error message?
What class file was involved?
You're posting an error message but I have no idea what you are doing to get that message.
The code you posted will not compile. You should not be able to execute it unless it compiles.
-
Re: Beginner for Java, need help with this code I wrote
Yes, that's what 've been saying.
The problem is that's all it says. All it tells me is that error code.
Also, it gets an error with the case default in the switch.
I have the feeling we aren't on the same page.
-
Re: Beginner for Java, need help with this code I wrote
What command are you executing that gets that error message?
Quote:
that's all it says. All it tells me is
What is the "it" you are talking about? Be more specific than "my PC"
-
About the default problem: You have not to use case to introduce the default case. Instead of writing
case default:
try simply
default:
-
Re: Beginner for Java, need help with this code I wrote
Ok, thanks, that solved the default problem, but now we still have the same error as before and a new problem earlier in the switch:
Code java:
switch ( (double) 3*Math.random() ) {
The (double) 3*Math.random() is underlined in red, and the error in Eclipse says
Cannot switch on a value of type double. Only convertible int values, strings or enum constants are permitted
Uh... help? And Norm, I am really confused. I cannot be more specific than my PC. It does not tell me where the error occurs NOR does it tell me what command I used to initiate the error. If you are thinking that I could use a try..catch thing, I have not yet attempted that with this code. I am sorry, but I am so utterly confused at what you are telling me.
-Silent
-
Re: Beginner for Java, need help with this code I wrote
Quote:
switch on ... Only convertible int values
Change the value to be one that is allowed, like an int.
-
Re: Beginner for Java, need help with this code I wrote
Yes, I tried that. When I switch the (double) to an (int), then it says the same thing and when I switch it to a string it says
The operator * is undefined for the argument type(s) String, double
It also won't work when I switch it to char.
-
Re: Beginner for Java, need help with this code I wrote
The switch statement will work with an int.
You need to write an expression that is an int value.
Try defining an int variable and assigning it a value from the expression you are using and then using the variable in the switch statement.
int var = <expression>
switch(var)
-
Re: Beginner for Java, need help with this code I wrote
Thank you, Norm, that solved the switch problem, but when I run the code in Eclipse, it still gives the main() error.
-
Re: Beginner for Java, need help with this code I wrote
What is the commandline that is being executed when you "run" the code?
Normally the commandline would be: java <classname>
For you the <classname> would be Cards.
-
Re: Beginner for Java, need help with this code I wrote
If I ran it in the console (cmd), it would be javac to compile and java to run it. If I run it in Eclipse, I suppose i just click the run button.
Is this what you've been asking?
-
Re: Beginner for Java, need help with this code I wrote
I was asking how you do run it. Not if you were to .....
What commandline is executed that gives you the error message? Your IDE is creating and using a commandline to execute the code. What is in that commandline when you "run" the code?
-
Re: Beginner for Java, need help with this code I wrote
Really, I'm sorry, I don't know what your talking about.
All I do when I run the program in Elipse to check to see if there are any errors is I click the run button.
Hmmmmmm.......
Can anyone else elaborate on Norm's message? I guess I'm a little too beginner to understand what he's trying to tell me.