Link to home
Start Free TrialLog in
Avatar of Ewe kean tan
Ewe kean tan

asked on

array output cant display ... num[0] incorrect ?what is the problem ?

 public static void main(String[] args) {
        String word;
        char y,a;
        int index;
         int[] num;
        Scanner input = new Scanner(System.in);
        do{
            
        
        System.out.println("Program to convert words to theirs corresponding telephone digits");
        System.out.println("Please enter an 10 character word :");
        word =input.nextLine();
          char phone[]=word.toCharArray();
          for(index = 0;index <word.length();index++){
             
              num =new int[index];
              switch (phone[index]){
                  case 'A':case'B':case'C':case'a':case'b':case'c':
                      
                      num[index]=0;
                      break;
                     case 'D':case'E':case'F':case'd':case'e':case'f': 
                         num[index]=1;
                      break;
                      case 'G':case'H':case'I':case'g':case'h':case'i':
                             num[index]=2;
                      break;
                       case 'J':case'K':case'L':case'j':case'k':case'l':
                             num[index]=3;
                      break;
                      case 'M':case'N':case'O':case'm':case'n':case'o':
                             num[index]=4;
                      break;
                       case 'P':case'Q':case'R':case'p':case'q':case'r':
                             num[index]=5;
                      break;
                       case 'S':case'T':case's':case't':
                             num[index]=6;
                      break;
                       case 'U':case'V':case'u':case'v':
                             num[index]=7;
                      break;
                       case 'W':case'X':case'w':case'x':
                             num[index]=8;
                      break;
                       case 'Y':case'Z':case'y':case'z':
                             num[index]=9;
                      break;
                       default:
                           System.out.println("Invalid input");
              }
          }
          
        System.out.println("Word \t\t Number");
        System.out.println("------------------");
         System.out.println(word+"\t\t"+num[0]+num[1]+num[2]+num[3]+num[4],+num[5]+num[6]+num[8]+num[9]);
                
         System.out.println("------------------");
        System.out.print("Do you wish to display another number [Y/N]?");
        y =input.next().charAt(0);
         }while(y =='Y'|| y =='y');
        System.out.println("BYE!");

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

what is the problem ?
You are meant to tell us. We are here to help you solve it
http://technojeeves.com/index.php/aliasjava1/17-errors
Avatar of Ewe kean tan
Ewe kean tan

ASKER

System.out.println(word+"\t\t"+num[0]+num[1]+num[2]+num[3]+num[4],+num[5]+num[6]+num[8]+num[9]);
this line ....got problem ..my switch cant take out the values
println takes ONE argument. You have given it two (there's a comma saying there are two)
sorry sir . i cant understand . i trying to put comma System.out.println(word+"\t\t",+num[0],+num[1],+num[2],+num[3],+num[4],+num[5],+num[6],+num[8],+num[9]);
           still wrong
Now you've given it 9 parameters instead of 1 (count the commas)
 public static void main(String[] args) {
        String word;
        char y;
        
        int index;
         String number="";
        Scanner input = new Scanner(System.in);
        
            
        
        System.out.println("Program to convert words to theirs corresponding telephone digits");
        do{
        System.out.println("Please enter an 10 character word :");
        word =input.nextLine();
          char phone[]=word.toCharArray();
         System.out.println("Word \t\t Number");
           System.out.println("------------------");
           
           System.out.print(word+"\t");
           int count =0;
          for(index = 0;index <word.length();index++){
            
          
              
              switch (phone[index]){
                  case 'A':case'B':case'C':case'a':case'b':case'c':
                      
                     System.out.print(0);
                      count++;
                      break;
                     case 'D':case'E':case'F':case'd':case'e':case'f': 
                       System.out.print(1);
                        count++;
                      break;
                      case 'G':case'H':case'I':case'g':case'h':case'i':
                        System.out.print(2);
                           count++;
                      break;
                       case 'J':case'K':case'L':case'j':case'k':case'l':
                             System.out.print(3);
                             count++;
                      break;
                      case 'M':case'N':case'O':case'm':case'n':case'o':
                             System.out.print(4);
                             count++;
                      break;
                       case 'P':case'Q':case'R':case'p':case'q':case'r':
                            System.out.print(5);
                             count++;
                      break;
                       case 'S':case'T':case's':case't':
                           System.out.print(6);
                             count++;
                      break;
                       case 'U':case'V':case'u':case'v':
                           System.out.print(7);
                           count++;
                      break;
                       case 'W':case'X':case'w':case'x':
                            System.out.print(8);
                             count++;
                      break;
                       case 'Y':case'Z':case'y':case'z':
                       System.out.print(9);
                            count++;
                           
                      break;
                       default:
                           System.out.println("Invalid input");
              }
              
          
          if(count==3){
              number +="-";
            
          }
          System.out.print(number);
          }   
            System.out.println("");
         System.out.println("------------------");
        System.out.print("Do you wish to display another number [Y/N]?");
        y =input.next().charAt(0);
         }while(y =='Y'|| y =='y');
        System.out.println("BYE!");
    }
    
}

Open in new window

now i using another format . now the problem are . i need to add hyphen "-"at first 3 digit number examples 013-30281723.expected output .
and also the do while statement .if i click y to display again the output .the output can only display .cant insert any input .
It would probably be a lot cleaner and use a lot less code if you collected future output and printed it once rather than printing it out ten times. Also it would cut down the amount of code if you just converted the input into lower case/upper case
I've just checked my house phone and A is 2 on my keypad. S comes with PQR and Z comes with WXY
013-30281723.expected output .
That would be 11 letters, not 10
if(count==3){
              number +="-";
           
          }
i just want add hyphen at first 3 number
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perhaps I'm a little confused. I'm not sure of the purpose, but it appears that you are trying to scan in a word, then based on the letters in that word that match the letters representing phone numbers, to output a phone number (or potential phone numbers). Could you provide a little more description of what it is you want do?
You are creating a new array num in every itteration of the cycle. And old inserted values in "num" are disapparing. Create num before the for loop.
num =new int[index]; should be outside of the for loop body
btw thank you all for helping .. i already solve it by myself
Step number one: please post the code you finished up with
mport java.util.Scanner;


public class Telephone_number {
             
                 
               
           
    public static void main(String[] args) {
        String word;
        char y;
        
        int index;
         String number="";
        Scanner input = new Scanner(System.in);
        
            
        
        System.out.println("Program to convert words to theirs corresponding telephone digits");
        do{
        System.out.println("Please enter an 10 character word :");
        word =input.nextLine();
          char phone[]=word.toCharArray();
         System.out.println("Word \t\t Number");
           System.out.println("------------------");
           
           System.out.print(word+"\t");
         
          for(index = 0;index <word.length();index++){
             
                  if(index==3){
              System.out.print("-");           
          }
          
         
              switch (phone[index]){
                  case 'A':case'B':case'C':case'a':case'b':case'c':
                      
                     System.out.print(0);
                      
                      break;
                     case 'D':case'E':case'F':case'd':case'e':case'f': 
                       System.out.print(1);
                        
                      break;
                      case 'G':case'H':case'I':case'g':case'h':case'i':
                        System.out.print(2);
                           
                      break;
                       case 'J':case'K':case'L':case'j':case'k':case'l':
                             System.out.print(3);
                             
                      break;
                      case 'M':case'N':case'O':case'm':case'n':case'o':
                             System.out.print(4);
                             
                      break;
                       case 'P':case'Q':case'R':case'p':case'q':case'r':
                            System.out.print(5);
                           
                      break;
                       case 'S':case'T':case's':case't':
                           System.out.print(6);
                             
                      break;
                       case 'U':case'V':case'u':case'v':
                           System.out.print(7);
                           
                      break;
                       case 'W':case'X':case'w':case'x':
                            System.out.print(8);
                             
                      break;
                       case 'Y':case'Z':case'y':case'z':
                       System.out.print(9);
                            
                           
                      break;
                       default:
                           System.out.println("Invalid input");
              }
              
          
        
          System.out.print(number);
          }   
            System.out.println("");
         System.out.println("------------------");
        System.out.print("Do you wish to display another number [Y/N]?");
        y =input.next().charAt(0);
        word =input.nextLine();
         }while(y =='Y'|| y =='y');
        
        System.out.println("BYE!");
        
    }
    
}

Open in new window