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: Help with swing GUI

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

    Default Help with swing GUI

    I wrote this program and it works perfectly running in the console. I want to use swing dialog boxes. I replaced all my console outputs with
    JOptionPanes. But I realize I need to find a way to store my iterations as Strings. How would I store each iteration in a String variable. Here
    is my code:

    import java.util.*;
    import javax.swing.*;
     
    public class TelephoneNumber2
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		String inputStr, lowerCase, preStr, postStr, phrase1;
    		int hyphenIndex, i, j = 0, k, l = 0, h, length, preLength, postLength, num = 0;
    		char character, character2;
     
    		//System.out.println("Please enter the phone number as a phrase(including hyphen): ");
    		JOptionPane jop = new JOptionPane();
     
    		inputStr = jop.showInputDialog("Please enter the phone number as a phrase(including hyphen): ");
     
    		//inputStr = console.next();
    		lowerCase = inputStr.toLowerCase();	
    		hyphenIndex = lowerCase.indexOf('-');
    		length = lowerCase.length();
    		preStr = lowerCase.substring(0, hyphenIndex);
    		postStr = lowerCase.substring(hyphenIndex + 1, length);
    		preLength = preStr.length();
    		postLength = postStr.length();
     
    		//System.out.print("The phone number is: ");
    		phrase1 = "The phone number is: ";
     
    		if(preLength == 3)
    		{
    		i = preLength - 1;
     
    		while(i >= 0)
    		{
    			character = preStr.charAt(j);
    			i--;
    			j++;
     
    			switch(character)
    			{
    				case 'a':
     
    				case 'b':
     
    				case 'c':
    					num = 2;
    					break;
     
    				case 'd':
     
    				case 'e':
     
    				case 'f':
    					num = 3;
    					break;
     
    				case 'g':
     
    				case 'h':
     
    				case 'i':
    					num = 4;
    					break;
     
    				case 'j':
     
    				case 'k':
     
    				case 'l':
    					num = 5;
    					break;
     
    				case 'm':
     
    				case 'n':
     
    				case 'o':
    					num = 6;
    					break;
     
    				case 'p':
     
    				case 'q':
     
    				case 'r':
     
    				case 's':
    					num = 7;
    					break;
     
    				case 't':
     
    				case 'u':
     
    				case 'v':
    					num = 8;
    					break;
     
    				case 'w':
     
    				case 'x':
     
    				case 'y':
     
    				case 'z':
    					num = 9;
    					break;	
    			}
     
     
    			//System.out.print(num);
    			jop.showMessageDialog(null, num, "tele", jop.INFORMATION_MESSAGE);
    		}
     
     
    			//System.out.print("-");
    			jop.showMessageDialog(null, "-", "tele", jop.INFORMATION_MESSAGE);
    			}
    			else
    			{
    				j = 0;
    				i = 2;
    				while(i >= 0)
    				{
    			character = preStr.charAt(j);
    			i--;
    			j++;
     
    			switch(character)
    			{
    				case 'a':
     
    				case 'b':
     
    				case 'c':
    					num = 2;
    					break;
     
    				case 'd':
     
    				case 'e':
     
    				case 'f':
    					num = 3;
    					break;
     
    				case 'g':
     
    				case 'h':
     
    				case 'i':
    					num = 4;
    					break;
     
    				case 'j':
     
    				case 'k':
     
    				case 'l':
    					num = 5;
    					break;
     
    				case 'm':
     
    				case 'n':
     
    				case 'o':
    					num = 6;
    					break;
     
    				case 'p':
     
    				case 'q':
     
    				case 'r':
     
    				case 's':
    					num = 7;
    					break;
     
    				case 't':
     
    				case 'u':
     
    				case 'v':
    					num = 8;
    					break;
     
    				case 'w':
     
    				case 'x':
     
    				case 'y':
     
    				case 'z':
    					num = 9;
    					break;	
    			}
     
     
    			//System.out.print(num);
    			jop.showMessageDialog(null, num, "tele", jop.INFORMATION_MESSAGE);
     
     
    		}
    			//System.out.print("-");
    			jop.showMessageDialog(null, "-", "tele", jop.INFORMATION_MESSAGE);
    			}
     
    			h = preLength - 4;
    			j = 3;
     
    			while(h >= 0)
    		{
    			character = preStr.charAt(j);
    			h--;
    			j++;
     
    			switch(character)
    			{
    				case 'a':
     
    				case 'b':
     
    				case 'c':
    					num = 2;
    					break;
     
    				case 'd':
     
    				case 'e':
     
    				case 'f':
    					num = 3;
    					break;
     
    				case 'g':
     
    				case 'h':
     
    				case 'i':
    					num = 4;
    					break;
     
    				case 'j':
     
    				case 'k':
     
    				case 'l':
    					num = 5;
    					break;
     
    				case 'm':
     
    				case 'n':
     
    				case 'o':
    					num = 6;
    					break;
     
    				case 'p':
     
    				case 'q':
     
    				case 'r':
     
    				case 's':
    					num = 7;
    					break;
     
    				case 't':
     
    				case 'u':
     
    				case 'v':
    					num = 8;
    					break;
     
    				case 'w':
     
    				case 'x':
     
    				case 'y':
     
    				case 'z':
    					num = 9;
    					break;	
    			}
     
     
    			//System.out.print(num);
    			jop.showMessageDialog(null, num, "tele", jop.INFORMATION_MESSAGE);
    		}
     
    			k = 6 - preLength;
     
    			while(k >= 0)
    			{
    				character2 = postStr.charAt(l);
    				k--;
    				l++;
     
    				switch(character2)
    			{
    				case 'a':
     
    				case 'b':
     
    				case 'c':
    					num = 2;
    					break;
     
    				case 'd':
     
    				case 'e':
     
    				case 'f':
    					num = 3;
    					break;
     
    				case 'g':
     
    				case 'h':
     
    				case 'i':
    					num = 4;
    					break;
     
    				case 'j':
     
    				case 'k':
     
    				case 'l':
    					num = 5;
    					break;
     
    				case 'm':
     
    				case 'n':
     
    				case 'o':
    					num = 6;
    					break;
     
    				case 'p':
     
    				case 'q':
     
    				case 'r':
     
    				case 's':
    					num = 7;
    					break;
     
    				case 't':
     
    				case 'u':
     
    				case 'v':
    					num = 8;
    					break;
     
    				case 'w':
     
    				case 'x':
     
    				case 'y':
     
    				case 'z':
    					num = 9;
    					break;	
    			}
     
    			//System.out.print(num);
    			jop.showMessageDialog(null, num, "tele", jop.INFORMATION_MESSAGE);	
    			}
     
     
     
     
    	}	
     
    }


  2. #2
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with swing GUI

    But I realize I need to find a way to store my iterations as Strings.
    I don't understand what you meant by "iterations" here? Please explain in more detail.

    java memory
    Last edited by namhm; December 4th, 2011 at 06:54 PM.

Similar Threads

  1. Java swing app
    By luke88 in forum Java IDEs
    Replies: 1
    Last Post: March 19th, 2011, 09:49 PM
  2. Could need some help with Swing
    By Kerr in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2011, 10:11 AM
  3. IO-swing problem
    By haresh2906 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 07:00 AM
  4. Swing
    By waboke in forum AWT / Java Swing
    Replies: 2
    Last Post: November 3rd, 2010, 08:55 AM
  5. Text in Swing
    By whoismrsaxon in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:22 AM