简体   繁体   中英

Why can't I call a private variable in main method?

this code is supposed to receive a full name string example "Billy Bob Smith" in an input dialog box and output the initials as a monogram example "BBS" in a message dialog box. but for some reason the main method won't let me acces the fullName variable.

import javax.swing.*;

public class HardMonogram {
     //---------- ATTRIBUTES ----------//
     private String fullName;
     private String monogram;
     private String first;
     private String middle;
     private String last;


     //---------- METHODS ----------//
     public String getInitial(String seperateName) {
           return seperateName.substring(0, 1);
     }

     public void getSeperateName(String fullName) {
           first  = fullName.substring(0, fullName.indexOf(" "));
           middle = fullName.substring(fullName.indexOf(" ") + 1, fullName.length());
           last   = middle.substring(middle.indexOf(" ") + 1, middle.length());
           middle = middle.substring(0, middle.indexOf(" "));
     }

    public void setMonogram() {
          monogram = getInitial(first)  +
                     getInitial(middle) +
                     getInitial(last);

    JOptionPane.showMessageDialog(null, monogram);
    }

    public static void main(String[] args) {
           myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");

           HardMonogram myMono = new HardMonogram();
           myMono.getSeperateName(myMono.fullName);
           myMono.setMonogram();

    }


}

gives me this build error

/Users/aaron/School/Fall 2012/CSCI-C 201/Labs/LB08/HardMonogram.java:33: error: cannot find symbol
    myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");
    ^
symbol:   variable myMono
location: class HardMonogram
1 error
[Finished in 1.2s with exit code 1]

it's for my intro to java class but I don't know why I can't acces the variable. I'm obviously overlooking something. any ideas?

Update:

After another read of question, you just need to move first line in main method after instance creation.

       HardMonogram myMono = new HardMonogram();
       myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");
       myMono.getSeperateName(myMono.fullName);
       myMono.setMonogram();

Simply put myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name"); after object declaration ( HardMonogram myMono = new HardMonogram(); ).

MyMono has not been declared in the first line of your main method. Add it to the beginning.

public static void main(String[] args) {
      HardMonogram myMono = new HardMonogram();
      myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");
      myMono.getSeperateName(myMono.fullName);
      myMono.setMonogram();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM