Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me figure out why I\'m getting the following errors. It tells me, \"

ID: 3744540 • Letter: P

Question

Please help me figure out why I'm getting the following errors. It tells me, " sc cannot be resolved" in the UWFLTranslator Tester.java Thank you.

import java.util.Scanner;

import java.util.StringTokenizer;

public class UWFLTranslator {

//private instance variable

private String vowels;

//default constructor

UWFLTranslator(){

vowels = "aeiou";

}

//Parameterized constructor, accepts a String, rep. letters that count as vowels

UWFLTranslator(String vowels){

this.vowels = vowels;

}

//public method, take a String, translate UWFLang

public String translateString(String word){

//orig. string is tokinzed into word

StringTokenizer string = new StringTokenizer(word);

String answer = "";

for(int i = 1; string.hasMoreTokens(); i++) {

//call the helper method translateWord with each word.

answer = answer + " " + translateWord(string.nextToken().toLowerCase());

}

return answer;

}

//private helper method; will take String repn word to be transl. to UWFLang

String translateWord(String word) {

String newWord = "";

String vowel = "";

String consonant = "";

//1st if stmt: Check if the word's first character is a vowel

if(vowels.indexOf(word.charAt(0)) != -1) {

newWord = "h";

for(int i = 0; i < word.length(); i++) {

//2nd it stmt:

if(vowels.indexOf(word.charAt(i)) != -1)

vowel += word.charAt(i);

else

consonant += word.charAt(i);

}

newWord += vowel + consonant + "uwf";

}

else {

newWord = word + "argo";

}

return newWord;

}

//Getter and setter method

void setVowels(String vowels) {

this.vowels = vowels;

}

String getVowels() {

return this.vowels;

}

}

***********************************

import java.util.Scanner;

public class UWFLTranslatorTester {

public static void main(String[] args) {

//scanner class for taking user input

Scanner scan = new Scanner(System.in);

System.out.println("Welcome to the UWF Language Translator");

//this is an infinite loop until user exits

while(true) {

//take string from user

System.out.print(" Enter a String to translate:");

String string = sc.nextLine();

//if it is a string, exit

if("exit".equals(string)) {

System.out.println("Goodbye! Good luck and...");

System.out.println("sayargo yourargo prayersargo littleargo hoenuwf dontargo forgetargo myargo sonargo " +

"toargo hiuenclduwf heeoevrynuwf ");

break;

}

//taking the vowels

System.out.println("Ener a String to represent vowels:");

String vowel = sc.nextLine();

//create object of UWFLTranslator class with vowel

UWFLTranslator object = new UWFLTranslator(vowel);

//and call the translateString

System.out.println("Translated String:" + object.translateString(string));

}

}

}

Explanation / Answer

The reason for getting error is that you have declared the Scanner object variable as scan but referencing it as sc in all places, like sc.nextLine().
So changing the declaration to Scanner sc = new Scanner(System.in); fixed the problem. Please do rate the answer if it helped. thank you.

------------------------------------------

import java.util.Scanner;

public class UWFLTranslatorTester {

public static void main(String[] args) {

//scanner class for taking user input
Scanner sc = new Scanner(System.in);

System.out.println("Welcome to the UWF Language Translator");

//this is an infinite loop until user exits

while(true) {

//take string from user
System.out.print(" Enter a String to translate:");
String string = sc.nextLine();

//if it is a string, exit

if("exit".equals(string)) {
System.out.println("Goodbye! Good luck and...");
System.out.println("sayargo yourargo prayersargo littleargo hoenuwf dontargo forgetargo myargo sonargo " +
"toargo hiuenclduwf heeoevrynuwf ");
break;

}

//taking the vowels

System.out.println("Ener a String to represent vowels:");
String vowel = sc.nextLine();

//create object of UWFLTranslator class with vowel
UWFLTranslator object = new UWFLTranslator(vowel);

//and call the translateString
System.out.println("Translated String:" + object.translateString(string));

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote