Reputation: 63
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?
Upvotes: 1
Views: 1908
Reputation: 66647
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();
Upvotes: 5
Reputation: 310
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();
}
Upvotes: 0
Reputation: 3710
Simply put myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");
after object declaration (HardMonogram myMono = new HardMonogram();
).
Upvotes: 4