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
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
[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 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...
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 writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
I have a recursive Tower of Hanoi program but don't know how to include the count...
I have a recursive Tower of Hanoi program but don't know how to include the count variable. Could you tell me how? Main.java import java.io.*; import java.util.List; public class Main { //int count = 0; /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk.*/...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT