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 2 of 2

Thread: GUI program won't take 'char' input code, can't seem to find the correct one...

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question GUI program won't take 'char' input code, can't seem to find the correct one...

    Hello. I have a program that takes a letter of the alphabet and translates it to a number on a telephone key pad. Everything is correct and prints out great in the console, but I need to do a GUI version of it, and I'm having massive trouble trying to figure out what the code is for the char user input.
    I've tried to put in things like

    userLetter = Parse.charParse (
            JOptionPane.showInputDialog("Please enter letter...");

    but it's not working, and I have no other clue what it could be. The console version is below, I just need help with what I need to change the
    "char userLetter =(QWERTY.next()).charAt(0);" to in order to get a GUI input box version. I know how to change the rest. Thanks in advance!

    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class Letter {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner QWERTY = new Scanner(System.in);
    		System.out.println("Enter a letter of the alphabet to see what number it corresponds to.");
     
     
    		char userLetter =(QWERTY.next()).charAt(0);
     
    		char letter = 0;
    		letter = (char) Character.toUpperCase(userLetter);
     
    		int Digit = 0;
     
    			if (letter == 'A' || letter == 'B' || letter == 'C') {
    			Digit = 2;
    			}
     
    			else if (letter == 'D' || letter == 'E' || letter == 'F') {
    				Digit = 3;
    			}
     
    			else if (letter == 'G' || letter == 'H' || letter == 'I') {
    				Digit = 4;
    			}
     
    			else if (letter == 'J' || letter == 'K' || letter == 'L') {
    				Digit = 5;
    			}
     
    			else if (letter == 'M' || letter == 'N' || letter == 'O') {
    				Digit = 6;
    			}
     
    			else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
    				Digit = 7;
    			}
     
    			else if (letter == 'T' || letter == 'U' || letter == 'V') {
    				Digit = 8;
    			}
     
    			else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
    				Digit = 9;
    			}
     
     
    		if (Digit==1 || Digit==2 || Digit==3 || Digit==4 || Digit==5 || Digit==6 || +
    				Digit==7 || Digit==8|| Digit==9) {
     
    			System.out.println("The corresponding number is = " + Digit);
    		}
     
    		else {
    			System.err.println("Error. Please try again.");
    		}
     
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: GUI program won't take 'char' input code, can't seem to find the correct one...

    Recommended reading: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Input a single char.Comparisson failed
    By Samaras in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 26th, 2012, 08:12 AM
  2. Can anybody correct the code?
    By ur2cdanger in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 24th, 2011, 12:50 AM
  3. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM
  4. How to check if a user input is a char or a int
    By Blackbird94 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 8th, 2011, 04:17 PM
  5. Replies: 2
    Last Post: August 2nd, 2011, 08:11 AM