-1

Hi so im trying to do this pig latin code in which the first letter is deleted from a words and sent to the end of the word while adding an "ay" to the end result. I have finished all my code in which it should output the right result except my problem is with actually outputting the code. I get an error message right after the user inputs the sentence.

Here is my code:

package piglatin;

  import java.util.Scanner;

  public class PigLatinTest {
 public  static String str;
 public static String[] words;

 public static String[] printLatinWords()
 {
     System.out.println("Enter a Sentence: ");
       Scanner scanner = new Scanner(System.in);
       str = scanner.nextLine();
       words = str.split(" ");
       //System.out.println(words);
       return words;
 }

 public static String[] printPigLatinWords() 
 {
    for (int i = 0; i < words.length; i++) {
        char firstLetter = words[i].charAt(0);
        words[i] = words[i].substring(1);
        words[i] = words[i] + firstLetter + "ay";
        //System.out.println(words[i]);
        //If you want the words to be in the same line, then this could help                  instead of System.out.println:
        System.out.print(words[i] + " ");
    }
    return words;

 }

public static void main(String[] args) 
{
    words = printLatinWords();
}

  }

  /*
   Enter a Sentence: 
    Hello from the other side 
     */

3 Answers3

3

You have static members and you shadow them with local variables. You are setting the values of the local variables, while your static members remain uninitialized. Suggestion:

public static String[] printLatinWords()
{
    System.out.println("Enter a Sentence: ");
      Scanner scanner = new Scanner(System.in);
      str = scanner.nextLine();
      words = str.split(" ");
      //System.out.println(words);
      return words;
}

Of course, words must be an array instead of a String then:

public static String[] words;

EDIT:

Since you have an array of String items, you need to iterate it:

public static String[] printPigLatinWords() 
{
   for (int i = 0; i < words.length; i++) {
       char firstLetter = words[i].charAt(0);
       words[i] = words[i].substring(1);
       words[i] = words[i] + firstLetter + "ay";
       System.out.println(words[i]);
       //If you want the words to be in the same line, then this could help instead of System.out.println:
       //System.out.print(words[i] + " ");
   }
   return words;

}

EDIT2:

main method suggestion:

public static void main(String[] args) 
{
    printLatinWords();
    printPigLatinWords();
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • but then in my second method how would i delete the first letter and add it to the end if it is a array? – JustAJAVAGUY Feb 04 '17 at 09:29
  • @JustAJAVAGUY see my edit. – Lajos Arpad Feb 04 '17 at 09:35
  • I just tried that and it gets stuck after taking in the input for some reason. What goes inside the main method? I think there's something wrong with the main method – JustAJAVAGUY Feb 04 '17 at 09:38
  • @JustAJAVAGUY what do you mean it gets stuck? It should not get stuck. Maybe the problem is that the words are not printed? You can call System.out.println(words[i]); after words[i] = words[i] + firstLetter + "ay"; Or do you get any errors? – Lajos Arpad Feb 04 '17 at 09:44
  • @JustAJAVAGUY the main method just executes the other two methods, so the problem should not be there. – Lajos Arpad Feb 04 '17 at 09:45
  • thanks for taking the time to help me, I dont get any errors but when i put " System.out.println(words);" into the main i get " [Ljava.lang.String;@55f96302" as the output – JustAJAVAGUY Feb 04 '17 at 09:46
  • @JustAJAVAGUY you need to print words[i], not words. I will edit my answer to reflect that. – Lajos Arpad Feb 04 '17 at 09:47
  • when i run it after the edit i get null as the output immediately without even asking for user input. – JustAJAVAGUY Feb 04 '17 at 09:54
  • @JustAJAVAGUY do you get any errors? Can you edit your question, add the code as it is now and describe the exact behavior? If so, please comment here when you are done. – Lajos Arpad Feb 04 '17 at 09:56
  • i have edited my code, once i input the sentence, nothing happens after that – JustAJAVAGUY Feb 04 '17 at 10:01
  • @JustAJAVAGUY you are not calling printPigLatinWords in your main method, therefore it is not executed. – Lajos Arpad Feb 04 '17 at 10:03
  • Thank you so much i just got it... i couldn't have done it without you. – JustAJAVAGUY Feb 04 '17 at 10:06
  • @JustAJAVAGUY no problem, I was happy to help. Good luck with your future projects! – Lajos Arpad Feb 04 '17 at 10:06
1

Assuming the error is what I think it is, you are not setting your public static variables anywhere (neither str nor words). Rather, you are setting local variables in your printLatinWords method.

I suggest you do one of two things:

  • Get rid of the local variables in that method where they have the same name as the global variables, or
  • Pass arguments to your methods and make use of their return values.
Joe C
  • 15,324
  • 8
  • 38
  • 50
  • Exception in thread "main" java.lang.NullPointerException at piglatin.PigLatinTest.printPigLatinWords(PigLatinTest.java:21) at piglatin.PigLatinTest.main(PigLatinTest.java:33) - this is my error – JustAJAVAGUY Feb 04 '17 at 09:26
1

try

inside your main() method

words = printLatinWords();

inside you printPigLatinWords() method

String word = words[0];
char firstLetter = word.charAt(0);
   word = word.substring(1);
   word = words[0] + firstLetter + "ay";
   return word;
Majeed Khan
  • 505
  • 7
  • 16