Results 1 to 4 of 4

Thread: Java is not setting the strings?

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Java is not setting the strings?

    So I am just testing stuff out, and lets say I want to set a string to something, for example:

    java Code:
    public class Party {
        String Name;
        String Event;
        public void DisplayInfo( ) {
            System.out.println("Happy" + Event + Name + "!");
        }
       
        public void setInfo(String PName, String vent) {
            PName = Name;
            vent = Event;
        }
        public static void main(String[] args) {
            Party celebrate = new Party();
            celebrate.setInfo("Officer Barbrady","Birthday");
            celebrate.DisplayInfo( );
        }

    }

    Lets make it display Happy Birthday Officer Barbrady!

    But that won't work the output is: Happynullnull!

    What am I doing wrong?

  2. #2
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    You have it reversed. It should be Name = PName, not PName = Name.

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    You have it reversed. It should be Name = PName, not PName = Name.
    Ahh TY

  4. #4
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    If you want to save yourself having to create a new object, you can just add static modifiers to both variables and methods involved in the static reference. Just a little bit of useful java info

    java Code:
    public class Party {
       
        static String Name;
        static String Event;

        public static void DisplayInfo() {
            System.out.println("Happy" + Event + Name + "!");
        }

        public static void setInfo(String PName, String vent) {
            Name = PName;
            Event = vent;
        }

        public static void main(String[] args) {
            setInfo("Officer Barbrady", "Birthday");
            DisplayInfo();
        }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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