Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: Beginner for Java, need help with this code I wrote

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  3. The Following User Says Thank You to Norm For This Useful Post:

    SilentNite17 (February 12th, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  6. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Beginner for Java, need help with this code I wrote

    ? 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.

    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)
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. The Following User Says Thank You to snowguy13 For This Useful Post:

    SilentNite17 (February 12th, 2012)

  8. #6
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by SilentNite17; February 12th, 2012 at 02:08 PM. Reason: Forgotten additional info and Thanks

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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

    "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.

    because they never compile.
    The compiler prints out error messages describing the errors that it finds. For example:
    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.
    Last edited by Norm; February 12th, 2012 at 02:14 PM.

  10. #8
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner for Java, need help with this code I wrote

    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)

  12. #10
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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?

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner for Java, need help with this code I wrote

    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

  14. #12
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for Java, need help with this code I wrote

    Alright, here:

    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
     
     
    		}
     
     
    	}

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  16. #14
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner for Java, need help with this code I wrote

    What command are you executing that gets that error message?

    that's all it says. All it tells me is
    What is the "it" you are talking about? Be more specific than "my PC"

  18. #16
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    About the default problem: You have not to use case to introduce the default case. Instead of writing
    case default:
    try simply
    default:

  19. #17
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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:

    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

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner for Java, need help with this code I wrote

    switch on ... Only convertible int values
    Change the value to be one that is allowed, like an int.

  21. #19
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  22. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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)

  23. #21
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  25. #23
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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?

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  27. #25
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. (beginner) if else code.
    By dumb1101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 16th, 2012, 02:47 AM
  2. Beginner pleasee help with java code
    By chuy525 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 06:38 PM
  3. Replies: 3
    Last Post: October 19th, 2011, 11:55 PM
  4. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM
  5. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM