3.14.1: Warm up: Text message abbreviation decoder (Java)

In summary: However, by including all the relevant information, you might be able to find someone who can help you out.
  • #1
zatawave
2
0
I'm working on this code for my programming class and I'm completely stuck. Here is what I have so far but I keep getting error messages.

Code:
import java.util.Scanner;

public class TextMsgAbbreviation {
   public static void main(String[] args)
   {
      String BFF ="best friends forever";
      String IMHO ="in my humble opinion";
      String TMI = "too much information";
      String LOL = "laughing out loud";
      String IDK="I don't know";
      
      Scanner=new Scanner(System.in);
      String textMsg="";
      
      System.out.println("Input an abbreviation:"+" ");
      
      textMsg=txtMsg.nextLine();
      
      if (textMsg.compareTo("BFF")==0)
   {
      System.out.println(BFF);
   }
      else if(textMsg.compareTo("IMHO")==0)
   {
      System.out.println(IMHO);
   }
      else if(textMsg.compareTo("TMI")==0)
   {
      System.out.println(TMI);
   }
      else if(textMsg.compareTo("LOL")==0)
   {
      System.out.println(LOL);
   }
      else if(textMsg.compareTo("IDK")==0)
   {
      System.out.println(IDK);
   }
      else
   {
      System.out.println("Unknown");
   }
      return;
   }
}

this is what I'm receiving once I run the code.

TextMsgAbbreviation.java:12: error: cannot find symbol
Scanner=new Scanner(System.in);
^
symbol: variable Scanner
location: class TextMsgAbbreviation
TextMsgAbbreviation.java:17: error: cannot find symbol
textMsg=txtMsg.nextLine();
^
symbol: variable txtMsg
location: class TextMsgAbbreviation
2 errors

Any help would be greatly appreciated.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Hi, and welcome to the forum!

Your problem is in the this line:

[m]Scanner=new Scanner(System.in);[/m]

Variable declaration and initialization looks as follows:

[m]ClassName varName = new ClassName(constructorArguments);[/m]

You omitted the variable name. Later you refer to this variable as [m]txtMsg[/m] in [m]txtMsg.nextLine()[/m], but I recommend naming it something like [m]input[/m]. It better conveys the idea that it stores the program's standard input, and it is less likely to be confused with [m]textMsg[/m].

Comparing strings with [m]compareTo[/m] is fine, but this method also compares strings lexicographically. You need only test for equality, so you could write

[m]if (textMsg.equals("BFF"))[/m]

You can also write the branching part as follows.

Code:
    switch (textMsg) {
      case "BFF": System.out.println(BFF); break;
      case "IMHO": System.out.println(IMHO); break;
      case "TMI": System.out.println(TMI); break;
      case "LOL": System.out.println(LOL); break;
      case "IDK": System.out.println(IDK); break;
      default: System.out.println("Unknown");
    }
Apparently, only string literals (i.e., string in quotes) can be used inside [m]switch[/m], but this is the case with your code.

Some hints for the future: write the whole question in the message body, not in the thread title. Also, you did well to include the precise error message rather than saying "It does not work", but in asking about a piece of code it is important to describe what it is supposed to do. It is also a good idea to shorten it as much as possible to keep the problem but exclude everything else. Determining what a piece of code does is what is called an undecidable problem: there is no computer algorithm that takes any code and determines what it does, whether it contains a logical error, etc. Naturally, it is hard for humans, too.
 

Related to 3.14.1: Warm up: Text message abbreviation decoder (Java)

1. What is the purpose of the "3.14.1: Warm up: Text message abbreviation decoder" program?

The purpose of this program is to decode common text message abbreviations and translate them into their full meanings. This can be useful for those who are not familiar with these abbreviations or for those who want to improve their understanding of modern communication.

2. What programming language is used for this program?

This program is written in Java, which is a popular object-oriented programming language commonly used for developing desktop, web, and mobile applications.

3. Can this program handle all text message abbreviations?

No, this program is designed to handle a specific set of common abbreviations. It may not be able to decode all abbreviations, as new ones are constantly being created and used in communication.

4. Is this program user-friendly?

Yes, this program is designed to be user-friendly. It has a simple interface and clear instructions for use.

5. Can this program be modified for other languages?

Yes, this program can be modified for other languages by adding the relevant abbreviations and their translations. However, since it is written in Java, it may require some programming knowledge to make these modifications.

Similar threads

  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
3
Views
5K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
836
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
843
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top