Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with methods

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Help with methods

     
    public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner (System.in);
     
            float x;
            int y;
            char ch1, ch2;
            String name;
            String input;
     
     
           input = JOptionPane.showInputDialog("Enter a number: "); // int y
           y = Integer.parseInt(input);
     
           name = JOptionPane.showInputDialog("Enter a name: "); // String name
     
           input = JOptionPane.showInputDialog("Enter a floating point value: "); // Float x 
           x = Float.parseFloat(input); 
     
     
           JOptionPane.showMessageDialog(null, "ch1 = " + ch1 + "y = " + y + "\nName is " + name + "\nx = " + x);
           System.exit(0);
           // output 
     
        }

    So far so good on this part ^^

    But, I also have a question about "char ch1 and ch2". I am not sure how to convert the char to a string.

    System.out.print("Enter a character: "); 
    input = keyboard.next(); 
    ch1 = input.charAt(0);
     
     
    to 
     
     
    JOptionPane.showInputDialog("Enter a character: "); 
    input = keyboard.next(); 
    ch1 = input.charAt(0);

    I know that ^^ is wrong just not sure where to go from the original code.
    Last edited by CSUTD; October 8th, 2011 at 04:01 PM.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Help with methods

    I could give you the code but that wouldn't help you. Here is a link. Go here and find the String class. It lists all the methods there are in the String class the parameters each method takes. There is a method that accomplishes what you are looking to do. If you have any more questions just post a reply.

    link: Java Platform SE 7

    hope it helps!

  3. The Following User Says Thank You to mwr76 For This Useful Post:

    CSUTD (October 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Help with methods

    I figured it out

    Final code:

    public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner (System.in);
     
            float x;
            int y;
            char ch1, ch2;
            String name;
            String input;
     
           input = JOptionPane.showInputDialog("Enter a character: "); // ch1
           ch1 = input.charAt(0); 
           input = JOptionPane.showInputDialog("Enter a number: "); // int y
           y = Integer.parseInt(input);
           input = JOptionPane.showInputDialog("Enter a character: "); // ch2 
           ch2 = input.charAt(0); 
           name = JOptionPane.showInputDialog("Enter a name: "); // String name
           input = JOptionPane.showInputDialog("Enter a floating point value: "); // Float x 
           x = Float.parseFloat(input); 
     
     
           JOptionPane.showMessageDialog(null, "ch1 = " + ch1 + "\ny = " + y + "\nch2 = " + ch2 + 
                   "\nName is " + name + "\nx = " + x);
           System.exit(0);
     
     
        }

  5. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help with methods

    Thanks CSUTD, always great when opening-posters put their final solutions on show, as It may help users with similar issues in the future.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. The Following User Says Thank You to newbie For This Useful Post:

    CSUTD (October 8th, 2011)

  7. #5
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Help with methods

    Thanks, glad to help.

Similar Threads

  1. Instantiation from within Methods
    By John Davies in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 04:39 PM
  2. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  3. 1st time using methods
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 1st, 2010, 02:37 PM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM