Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,157 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,524 people online right now. Registration is fast and FREE... Join Now!




converting a word into a telephone number

 

converting a word into a telephone number

over5

7 Nov, 2009 - 08:08 AM
Post #1

New D.I.C Head
*

Joined: 6 Nov, 2009
Posts: 6

this code is supposed to give me a telephone when i enter a word, but every time i try to run it i get this is an illegal expression.
CODE

import java.util.*;
import java.util.Scanner;

public class telPhone
{
    public static void main (String[] args)
    {
        
        Scanner scan = new Scanner (System.in);
    

        String number;
        String word;
        int i;
  
        
        public telPhone (word)
        {
            setNumber;
        }
        
        private void setNumber
        {
            number = "";
        }
    
    System.out.println("Enter your word:");
    word = scan.next();
    number= word.toUpperCase();


    for (i = 0; i < 7; i++)
     if (number.charAt(i) == A && B && C)
    {
        System.out.print("2");
    }
    else
    if (number.charAt(i) == D && E && F)
    {
        System.out.print("3");
    }
    if (number.charAt(i) == G && H && I)
    {
        System.out.print("4");
    }
    else
    if (number.charAt(i) == J && K && L)
    {
        System.out.print("5");
    }
    else
    if (number.charAt(i) == M && N && O)
    {
        System.out.print("6");
    }
    else
    if (number.charAt(i) == P && R && S)
    {
        System.out.print("7");
    }
    else
    if (number.charAt(i) == T && U && V)
    {
        System.out.print("8");
    }
    else
    if (number.charAt(i) == W && X && Y)
    {
        System.out.print("9");
    }
    
    number = number.substring(0,3) + "-" +
                       number.substring (3);
                      
      
    System.out.println("Your converted number is:" + word);

}
}


User is offlineProfile CardPM
+Quote Post


macosxnerd101

RE: Converting A Word Into A Telephone Number

7 Nov, 2009 - 08:30 AM
Post #2

Self-Trained Economist
Group Icon

Joined: 27 Dec, 2008
Posts: 1,884



Thanked: 208 times
Dream Kudos: 325
Expert In: Java

My Contributions
Two things to remember. One, characters are enclosed between single quotes. If you just say:
CODE

x.charAt(0) == A


Then the computer will look for a variable named A. Also, the following is an invalid expression:
CODE

x.charAt(0) == A && B && C


You can't use the conditional operators without boolean values on either side. And even once you correct the letters to add single quotes, they still won't be true or false values. I think what you wanted to do was test for any of the characters. So the syntax looks more like:
CODE

x.charAt(0) == 'A' || x.charAt(0) == 'B' || x.charAt(0) == 'C'


In this way, you are testing for 'A', 'B', or 'C'.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:38PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month