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();
}
}
You need to make the following changes:
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
Get Answers For Free
Most questions answered within 1 hours.