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.

Results 1 to 4 of 4

Thread: Having trouble with Character and Boolean

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble with Character and Boolean

    I am having trouble with an assignment, the requirements being I have to make a madlib containing every single bit of info we have learned, but I am struggling with getting Boolean and character to work correctly

    These are the errors that pop up when I try to compile:
    --------------------------------------------------------------------------------------------------------------------------------------------
    C:\Users\jnhv6\Desktop\I wasn't paying attention\MadLib.java:44: error: variable rank is already defined in method main(String[])
    char rank = 'A';
    ^
    C:\Users\jnhv6\Desktop\I wasn't paying attention\MadLib.java:47: error: cannot find symbol
    rank = rank.nextChar();
    ^
    symbol: method nextChar()
    location: variable rank of type Scanner
    2 errors

    Tool completed with exit code 1
    --------------------------------------------------------------------------------------------------------------------------------------------
    And this is the actual code:
    --------------------------------------------------------------------------------------------------------------------------------------------
     import java.util.Scanner;
     public class MadLib
     {
    	 public static void main(String[]args)
    	 {
    		 //declare and initialize variables
    		 Scanner keyboard = new Scanner(System.in);
    		 String name = "";
     
    		 //Prompt the user for information
    		 System.out.println("What is your first name?");
    		 name = keyboard.next();
    		 keyboard.nextLine();
     
    		 //Print out MadLib
    		 System.out.println(name + " was walking through school one day"
    		 + " minding your own business");
     
     
    		 Scanner girl = new Scanner(System.in);
    		 int number = 0;
     
    		 System.out.println("When all of a sudden, (Choose a number)");
    		 number = girl.nextInt();
    		 girl.nextLine();
     
    		 System.out.println(number + " girls randomly swarmed you asking for your autograph and number");
     
    		 Scanner love = new Scanner (System.in);
    		 double meter = 0;
     
    		 System.out.println("They're love meters seem to be off the charts, clocking in at a whopping (Choose a number)");
    		 meter = love.nextDouble();
    		 love.nextLine();
     
    		 System.out.println(meter + " love-ometers per millisecond!");
    //----------------------------------------------------------------------------------------------------------------------
    		 Scanner rank = new Scanner(System.in);
    		 char rank = 'A';
     
    		 System.out.println("Turns out, you had involuntarily taken a compatibility test, and you scored a (Choose a Letter)");
    		rank = rank.nextChar();
    		 rank.nextLine();
     
    		 System.out.println(rank + " on the test, which was suprising to a lot of them!");
    //-----------------------------------------------------------------------------------------------------------------------
    		 Scanner great = new Scanner(System.in);
    		 System.out.println("Turns out, the actual results of the test were");
    		 boolean goof = false;
    		 goof = great.nextBoolean();
    		 great.nextLine();
     
    		 System.out.println(great +" so, that was nice to know");
    		 great.nextLine();
     
    		 System.out.println("GAME OVER");
    		 }
     
     
     }
    --------------------------------------------------------------------------------------------------------------------------------------------
    Please help, and thank you
    Last edited by Vingyl_Lygniv; October 4th, 2019 at 01:03 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,479
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Having trouble with Character and Boolean

    variable rank is already defined in method main(String[])
    There can only be one definition for a variable in a method.
       Scanner rank = new Scanner(System.in);    // defines rank as a Scanner object
       char rank = 'A';                            // defines rank as a char  <<<< Should use different name

    error: cannot find symbol
    rank = rank.nextChar();
    Part of that error message is missing. It should show what symbol was not found. Be sure to copy the full text of the message when posting.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with Character and Boolean

    I have reformatted and found the missing pieces, thank you for notifying

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,479
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Having trouble with Character and Boolean

    symbol: method nextChar()
    location: variable rank of type Scanner
    The compiler can not find that method in the Scanner class. Read the API doc for what methods the Scanner class has.
    https://docs.oracle.com/javase/8/docs/api/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading text file character by character into a 2d char[][] array
    By filipasa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2019, 07:09 PM
  2. [SOLVED] Boolean trouble, no idea whats wrong.
    By Cekeh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2013, 07:13 PM
  3. Boolean and String Trouble...
    By Souljahgirl101 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2012, 08:17 PM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM