7

This may seem like a silly question, but after going through pages of google, i havnt been able to find the answer i want.

s1.setName(JOptionPane.showInputDialog("Enter Name: ");

For the above piece fo code, how would i format the data the user entered to be all capitals?

Any help here would be appreciated.

user1081326
  • 415
  • 5
  • 10
  • 19

2 Answers2

9

The String class has a toUpperCase() method on it.

JOptionPane.showInputDialog(..) returns a String, so you can use:

JOptionPane.showInputDialog("Enter name: ").toUpperCase();
gregdferrell
  • 290
  • 3
  • 11
  • Thank You, I was a bit stupid. I was trying the .toUpperCase() everywhere but the end. – user1081326 Apr 25 '12 at 16:55
  • Remember that in Java, chained methods are always called left to right (absent parenthesis). So you would always want to have the method that changes the string to the right of the method that pulls down the string. – Charles Apr 25 '12 at 17:19
8

See String.toUpperCase()

Remember that String is immutable, so this creates a duplicate string

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80