#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Post Algorithim error messages


    I'm a beginning computer science student who needs a bit of help.

    The gist of my assignment is to create an algorithm that takes an input of a character like a, b, or c and output what number that letter would be on a telephone. For example ABC = 1 DEF=2 etc. I figured I would just do around 20 if else statements untill the whole thing was done and here's what I've come up with so far. But I'm getting lots of error messages that seem to tell me I'm going about this the wrong way.

    My code is this
    Code:
    import java.util.Scanner;
    
    public class Algorithims
    {
    public static void main (String[] args){
    
    char letter;
    int number;
    System.out.print("Enter your letter please);
    Scanner your_letter=new Scanner(System.in); 
    if (your_letter=a);
    {system.out.println ("Your number is 1");}
    else {system.out.println("Will do the other letters later");}
    }
    }
    I am getting a couple of error messages though.
    The first one is in line 11.
    It says it cannot find the symbol a. This is the most common error I encounter when working with java and I know it's very broad.

    The second error is also in line 11. The error is incompatible types

    The third error is in line 12. The error is , ERROR package system doesn't exist.

    Also when playing around with any code it seems like whenever I fix all the errors, more errors take their place
    Is this normal?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I'm getting lots of error messages
    Please post the full text of the error messages.

    One problem I see is your usage of the Scanner class to read in data from the user. You need to use one of the Scanner class's methods to read the data. See the API doc for Scanner for examples of how to use its methods.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    == compares the object memory references (are they literally the same object in the computers memory.

    What you actually want to do is use the equals method. This compares the value of the string.

    So:

    Code:
    String myString = new String("a");
    String myString2 = new String("a");
    
    if (myString == myString2)
    {
      // This is NOT true.
    }
    
    if (myString.equals(myString2))
    {
       // This IS true.
    }
    Have a look at this code

    Code:
    Scanner oScanner = new Scanner(System.in);
    		
    		System.out.print("Enter the letter: ");
    		String strLetter = oScanner.next();
    		System.out.println("Your input is: " + strLetter);
    		
    		if ("a".equals(strLetter) || "b".equals(strLetter) || "c".equals(strLetter))
    		{
    			System.out.println("This is key 2");
    		}
    		
    		if ("d".equals(strLetter) || "e".equals(strLetter) || "f".equals(strLetter))
    		{
    			System.out.println("This is key 3");
    		}
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0
    Originally Posted by doa24uk
    == compares the object memory references (are they literally the same object in the computers memory.

    What you actually want to do is use the equals method. This compares the value of the string.
    I get it. == is for numbers only then? Is there anyway to get it to work with characters though? I know strings can only be one character long but I thought to use char, since it was only one letter.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    You can use the == operator with the primitives like int, byte and char.
    For objects you need to use the equals() method.

    "A" is a String. 'A' is a char

IMN logo majestic logo threadwatch logo seochat tools logo