Results 1 to 5 of 5
  1. #1
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Post Error: The local variable city may not have been initialized

    School assignment. I haven't figured out what correction to make. Would appreciate any help. Thanks!

    Java Code:
    //use Scanner
    import java.util.Scanner;
    
    public class Stringmanipulator 
    {
      public static void main (String [] args) 
      {
        
          //declare input variables
          String city;
          String upper = city.toUpperCase();
          String lower = city.toLowerCase();
          char letter = city.charAt(0);
          int stringSize = city.length();
                
          //delcare a scanner
          Scanner kbrd;
       
          // create a scanner object attach it to the kbrd (keyboard)
          kbrd = new Scanner(System.in);
      
          //learn city
          System.out.println("What city do you live in?");
       
          //accept city
          city=kbrd.nextLine();
          
          System.out.println(stringSize);
          System.out.println(upper);    
          System.out.println(lower);    
          System.out.println(letter); 
          
      } 
       
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,597
    Rep Power
    5

    Default Re: Error: The local variable city may not have been initialized

    You are trying to access parts of the String stored in city. But the variable has not been assigned any string.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Error: The local variable city may not have been initialized

    Thank you Jim for responding. How would you (or anyone else) go about correcting? While incorrect. I thought line #10 & #26 addressed this problem.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,597
    Rep Power
    5

    Default Re: Error: The local variable city may not have been initialized

    Line number 10 only declares the variable type. It does not assign it a value.
    Line number 26 reads in a string. But you try to reference it starting in line 11, before you ever read it. You need to assign the variable a value before you try to access its contents.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Error: The local variable city may not have been initialized

    Got it, you're awesome! Thanks again

Similar Threads

  1. variable may not have been initialized...
    By ajetrumpet in forum New To Java
    Replies: 6
    Last Post: 03-24-2013, 11:35 AM
  2. Variable to initialized
    By Bungie in forum New To Java
    Replies: 10
    Last Post: 11-28-2012, 09:38 AM
  3. Local variable might not have been initialized
    By outforlunch in forum New To Java
    Replies: 1
    Last Post: 09-17-2012, 09:16 PM
  4. Replies: 4
    Last Post: 06-29-2012, 10:05 PM
  5. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •