My assignment is to use a method to do the pig latin part of a program I have already done. So basically, I have the program written, code is below, but am getting two errors in it. I have not really gone over using strings and returning them to the main program in my class yet, but after viewing some other questions on this site, I found some hints to what I needed to do for it. I thought I had done it right, but does not compile. This program has two errors.

Error #1 is with the output statement when I try to use the string variable 'word' at the end of the output statement. I am trying to use the string that is in the variable after the function runs. The error tells me that there is a syntax error on token "word" and I should delete this token.

Error #2 is with the actual function title. The compiler says there are errors on each end of the parameter, underlining both '(' and ')' in red, but not what is contained in the middle of them. The compiler tells me that there is a syntax error and that a semicolon is expected there.

Here is the code. Thanks in advance for any assistance in this matter. I have only been doing Java for about two months now.

import java.util.*;

public class PigLatinWithFunctions {
	{

		public static void main(String[] args) 
		{
		Scanner keyboard = new Scanner(System.in);
		String x, y, z;
		
		
		System.out.println("Enter a word :");
		x = keyboard.nextLine();
		PigLatin(x);
		
		
		System.out.println("The word you entered in pig latin is " + word);
		
		}
		public static String PigLatin(String x)
		{
			String word;			
			y = x.substring(1);
			z = x.substring(0, 1);
			word = y + z + "ay";
			return word;
		}
	}
}

To be more clear on where the errors are, error #1 is with 'word' in this line of code.

System.out.println("The word you entered in pig latin is " + word);

Error #2 has been fixed, but error number one remains in my program. Now is also tells me on top of other error above that "word cannot be resolved".

Nevermind on this problem, its solved. The issue was using the variable word for my output string. I changed 'word' to 'PigLatin(x)' to get the return output from the method directly into the output line.

To be more clear on where the errors are, error #1 is with 'word' in this line of code.

System.out.println("The word you entered in pig latin is " + word);

Error #2 has been fixed, but error number one remains in my program. Now is also tells me on top of other error above that "word cannot be resolved".

This is because your variable "word" is local to your PigLatin method and is not know in your main method. If you want to use "word" variable in main, then make it class variable.

This question has already been answered. Start a new discussion instead.