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

AI Thread Summary
The discussion centers on troubleshooting a Java program designed to expand text message abbreviations. The user encounters errors related to variable declaration and initialization. Specifically, the line "Scanner=new Scanner(System.in);" lacks a variable name, leading to a "cannot find symbol" error. Additionally, the user mistakenly refers to the Scanner variable as "txtMsg" when trying to read input, which is incorrect. Suggestions include renaming the Scanner variable to something more descriptive, such as "input," and using the "equals" method for string comparison instead of "compareTo." A more efficient approach using a switch statement is also recommended for handling the abbreviations. The importance of providing clear problem descriptions and including error messages in queries is emphasized for better assistance in future discussions.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top