Results 1 to 6 of 6

Thread: Output issue

  1. #1
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Output issue

    Hi guys, first off this is homework and i did throw this on another form but am still confused. Pretty much i have to take user input, turn it into uppercase letters that correspond with the phone keypad 2=ABC etc and translate whatever input is letters into those numbers. My program works, problem is when i add a space in my input ex.597 3450, i get back 597*32*3450, when i dont add a space the number comes back as it was put in. I am trying to figure out how to declare "space" but am stumped. Any ideas? Thanks in advance, sorry if i did not explain this too good.


    Java Code:
    package chapter_9;
    
    import java.util.Scanner;
    
    
    public class Nine_Seven {
    
        
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a string: ");
            String string = input.nextLine();
            string = string.toUpperCase();
            
            for (int i = 0;i<string.length();i++){
                if(Character.isDigit(string.charAt(i))) {
                    System.out.print(string.charAt(i));
                }  else
                    System.out.print(getNumber(string.charAt(i)));}
                
                    
        }
        
                    
        
         public static int getNumber(char uppercaseLetter){
             
            switch (uppercaseLetter) {
                case 'A':
                case 'B':
                case 'C':
                   return 2;
                case 'D':
                case 'E':
                case 'F':
                   return 3;
                case 'G':
                case 'H':
                case 'I':
                   return 4;
                case 'J':
                case 'K':
                case 'L':
                   return 5;
                case 'M':
                case 'N':
                case 'O':
                   return 6;
                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                   return 7;
                case 'T':
                case 'U':
                case 'V':
                   return 8;
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                   return 9;
                
            }
            return(uppercaseLetter);
         }
                
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Output issue

    What other forum? We'd prefer to see the link so as not to duplicate effort. We are all volunteers doing this on our free time after all.

  3. #3
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Output issue


  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Output issue

    Thank you. Your problem is with Character.isDigit(...). You don't want to exclude just digits, you want to exclude anything that is not ABCDEFGHIJKLMNOPQRSTUVWXYZ. So test for that. Test if "ABCDEFGHIJKLMNOPQRSTUVWXYZ" contains your character (look at the String class methods), and if so, call your method, otherwise display the character unchanged.

  5. #5
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Output issue

    Ok thank you for the help, i have been going round and round with this for what seems like too long, guess it may be the best way to learn though!

  6. #6
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Output issue

    I just wanted to thank you for your help, i finally got it right, i adjusted my if statements and it works great. Thanks again.

    Java Code:
    package chapter_9;
    
    import java.util.Scanner;
    
    
    public class Nine_Seven {
    
        
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a string: ");
            String string = input.nextLine();
            string = string.toUpperCase(); 
                    
           
            
            for (int i = 0;i<string.length();i++){
                if(Character.isAlphabetic(string.charAt(i))) {
                    System.out.print(getNumber(string.charAt(i)));}
                  else
                     System.out.print(string.charAt(i));}
       
    }           
        
         public static int getNumber(char uppercaseLetter){
             
            switch (uppercaseLetter) {
                case 'A':
                case 'B':
                case 'C':
                   return 2;
                case 'D':
                case 'E':
                case 'F':
                   return 3;
                case 'G':
                case 'H':
                case 'I':
                   return 4;
                case 'J':
                case 'K':
                case 'L':
                   return 5;
                case 'M':
                case 'N':
                case 'O':
                   return 6;
                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                   return 7;
                case 'T':
                case 'U':
                case 'V':
                   return 8;
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                   return 9;
                
            }
            return(uppercaseLetter);
         }
                
    }

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  2. Compiler Output Window Issue
    By sweetpea123 in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 05:56 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. Output Issue
    By jazz2k8 in forum New To Java
    Replies: 1
    Last Post: 08-04-2008, 10:56 AM
  5. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 10:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •