Results 1 to 6 of 6
  1. #1
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default A problem with String

    I have a string I declared in public

    When I try to use it in a private function I get a null:

    import acm.program.*;
    import java.util.*;

    public class Game extends ConsoleProgram {

    String userName1;

    public void run() {
    String userName1 = readLine("enter your name: ");

    private void Compare() {
    println(userName1 ) ;

    }
    }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: A problem with String

    you are redefine the String,

    ALSO Please put an Access type on your variables, make userName1 either public or private or protected, have you followed java tutorials?

    if you say

    String userName1;

    just say:

    userName1 = readLine("enter your name: ");

    You don't need to redefine it, thats the point of declaring the variable "Globally" you don't need to locally define it's type

  3. #3
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: A problem with String

    removing String = UserName1 results in errors in different places. I tried using public String = UserName1 but it also gives me errors...

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: A problem with String

    it should read:

    ALSO you don't have a constructor for game

    Java Code:
    import acm.program.*;
    import java.util.*;
    
    public class Game extends ConsoleProgram {
    
    private String userName1;
    
    
    public Game() {
    
    //constructor
    
    }
    
    public void run() {
    userName1 = readLine("enter your name: ");
    
    public void Compare() {
    System.out.println(userName1 ) ;
    
    }
    }
    The syntax is correct there, how ever your methods might not be.

    Doesn't seem to me like you have the very basics understood, have you transferred from another language that is more permissive?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default Re: A problem with String

    Quote Originally Posted by monkeyjr97 View Post
    you are redefine the String,

    ALSO Please put an Access type on your variables, make userName1 either public or private or protected, have you followed java tutorials?
    There is nothing wrong with the access type. A missing access type allows the second most restrictive access, second only to private. It is only visible within the same package.

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

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default Re: A problem with String

    If a class doesn't provide a constructor then the implied constructor will be used (i.e. the one you showed above). It is not necessary to provide an explicit constructor in a class declaration.

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

Similar Threads

  1. im have a string problem
    By flyhigh in forum New To Java
    Replies: 11
    Last Post: 01-24-2013, 06:08 PM
  2. Problem with <s:if> and string
    By SomeoneHere in forum Struts
    Replies: 1
    Last Post: 08-24-2012, 09:44 PM
  3. Having a problem with to String
    By thorobred in forum New To Java
    Replies: 20
    Last Post: 06-02-2012, 08:24 AM
  4. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  5. String Problem
    By gnomewise in forum New To Java
    Replies: 1
    Last Post: 10-19-2008, 12:37 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
  •