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

  • Context: Java 
  • Thread starter Thread starter zatawave
  • Start date Start date
  • Tags Tags
    Decoder Java Text
Click For Summary
SUMMARY

The discussion focuses on resolving errors in a Java program designed to decode text message abbreviations. The primary issues identified include the incorrect declaration of the Scanner object and the use of an undefined variable, leading to compilation errors. The recommended solutions involve properly naming the Scanner variable and utilizing the equals method for string comparison instead of compareTo. Additionally, a switch statement is suggested for cleaner code structure when handling multiple cases.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of variable declaration and initialization
  • String manipulation in Java
  • Control flow statements in Java (if-else and switch)
NEXT STEPS
  • Learn about Java Scanner class usage and best practices
  • Explore Java string comparison methods, specifically equals vs. compareTo
  • Study control flow structures in Java, focusing on switch statements
  • Review Java error handling and debugging techniques
USEFUL FOR

Java programming students, beginner developers, and anyone looking to improve their skills in error resolution and string manipulation in Java applications.

zatawave
Messages
2
Reaction score
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
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
6K
Replies
3
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K