Question

Here is my java code, I keep getting this error and I do not know how...

Here is my java code, I keep getting this error and I do not know how to fix it:

PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java
public class Main {
^

import java.io.*;

public class Main {

private static BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {

String english = getString();

String translated = translate(english);
System.out.println(translated);
}

private static String translate(String s) {
String latin = "";
int i = 0;
while (i<s.length()) {

while (i<s.length() && !isLetter(s.charAt(i))) {
latin = latin + s.charAt(i);
i++;
}

if (i>=s.length()) break;

int begin = i;
while (i<s.length() && isLetter(s.charAt(i))) {
i++;
}

int end = i;
latin = latin + pigWord(s.substring(begin, end));
}
return latin;
}

private static boolean isLetter(char c) {
return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
}

private static String pigWord(String word) {
int split = firstVowel(word);
if(split!=0){
return word.substring(split)+word.substring(0, split)+"ay";
}else{
return word+"yay";
}
}

private static int firstVowel(String word) {
word = word.toLowerCase();
int flag=0;
for (int i=0; i<word.length(); i++){
if (word.charAt(i)=='a' || word.charAt(i)=='e' ||
word.charAt(i)=='i' || word.charAt(i)=='o' ||
word.charAt(i)=='u' || word.charAt(i)=='y')
if(i==0 && word.charAt(0)=='y'){
flag=1;
}else{
return i;
}
if(flag==1){
continue;
}
}
return 0;
}

private static String getString() throws IOException {
return buf.readLine();
}
}

Homework Answers

Answer #1

You need to make the following changes:

  • Eliminate the first two lines, they are redundant. i.e
  • line1 - public class Main {
  • line2- ^

It may be possible that while copying your code on the platform, there was a mistake by your end. Nevertheless, please recheck if it is there on your code.

2. The main error is becasue of mismatch of names. The outer public class name doesnt match the name of your file.

Now you have named the class as public class Main {.....} . The name of your class is "Main". So the name of your java file (the file in which this code is present ) should/must also be Main.java.

When you make the above changes, the program will run successfully without any errors.

Please let me know if you are still facing any issues or have any questions/doubts etc. Thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What's wrong with this code? #Java Why I am getting error in : int BusCompare =...
What's wrong with this code? #Java Why I am getting error in : int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); public class TypeComparator implements Comparator<Type> { @Override public int compare(Type o1, Type o2) { // return o1.name.compareTo(o2.name); int NameCompare = o1.name.compareTo(o2.name); int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); // 2-level comparison using if-else block if (NameCompare == 0) { return ((BusCompare == 0) ? NameCompare : BusCompare); } else { return NameCompare; } } } public class Type implements Comparable<Type> { int id; String name; int...
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
1. Suppose that the integer variables n and m have been initialized. Check all the correct...
1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment. String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" ); Question 5 options: If m is strictly greater than 2n then the value of s2 is "two" If n is equal to m then the value of s2 is "two" If m is strictly greater than 2n then the value of s2 is "three" If...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
(JAVA) I have "cannot be resolved or is not a field" error on node.right = y;...
(JAVA) I have "cannot be resolved or is not a field" error on node.right = y; and node.left = x; //BuildExpressionTree.java import java.util.*; import javax.swing.*; import javax.xml.soap.Node; public class BuildExpressionTree {    private Stack stack = new Stack();    private Node node;       public static boolean isOperator(String token){        if(token == "+" || token == "-" || token == "*" || token == "/"){            return true;        }        JOptionPane.showMessageDialog(null, "Invalid token" +...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT