Results 1 to 5 of 5
  1. #1
    zygmfb is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Post Pig Latin translator

    Ok this program is supposed to change text files into pig latin but throws a NullPointerException when I run and enter the file name that should be scanned
    and it's due soon and I will lose points in class which are weighted heavily PLEASE help me

    Java Code:
    // your name, date
       import java.util.*;
       import java.io.*;
    	import java.lang.*;
        public class PigLatin_shell
       {
           public static void main(String[] args) throws FileNotFoundException
          {
             go();
             
          }
           public static String pig(String s)
          {
    			String strTempLetter = ""; //letter transfer from norm. array
    			String newerLetter = "";   //letter transfer for punc. array
    			
    			String tempSubstring = "";
    			String tempSubstring2 = ""; //substrings for both sides
    			String tempPunc = "";
    			String tempPunc2 = "";
    			
    			String newWordCaps = "";
    			
    			int wasUCase = 0;
    			int yWasFirst = 0;
    			
    			int indexOfLetter;
    			String newWord = "";
    			String [] tempWord = new String[s.length()]; //makes s letters into array
    			
    			
    			
    			for(int i = 0; i < s.length(); i++)
    			{				
    				strTempLetter = tempWord[i];		//.toLowerCase() //putting element letter into String and changes letter to lowercase
    				String uCaseCheck = "";
    				if(i == 0)
    				{
    					uCaseCheck = strTempLetter.toUpperCase(); //making a fake var uppercase to see if normal is upperCase
    					if(tempWord[0].equals(uCaseCheck))			 //returns true if first letter is upper case, makes it lowerCase
    					{
    						wasUCase = 1;		
    						strTempLetter = tempWord[0].toLowerCase();  //makes first letter lowerCase so it can run through program
    						
    						if(tempWord[0].equals("y"))
    							yWasFirst = 1;
    					}
    				}	
    																						//strTempLetter = strTempLetter.toLowerCase();
    				indexOfLetter = i; 				//getting index of the letter
    				
    				
    				if(strTempLetter.equals("a") || strTempLetter.equals("e") || strTempLetter.equals("i") || strTempLetter.equals("o") || strTempLetter.equals("u") || ((strTempLetter.equals("y") && !(yWasFirst == 1)))) //BASIC RULES AND Y
    				{
    					
    					if(strTempLetter.equals("u") && tempWord[indexOfLetter - 1].equals("q")) //SPECIAL checks if q is before u
    					{
    						tempSubstring = s.substring(0,indexOfLetter); 								 //getting last part of array before including u and q
    						tempSubstring2 = s.substring(indexOfLetter); 		 						 //first part of word before u, excluding u (which sends u to back)
    					
    						newWord = tempSubstring2 + tempSubstring + "ay";
    						
    						if(wasUCase == 1)   																			//if first letter uppercase
    						{
    							newWordCaps = newWord.substring(0,1);   										  //gets first letter of new word
    							newWordCaps = newWord.replace(newWordCaps, newWordCaps.toUpperCase()); //replaces first letter of new word with upperCase letter
    						}
    							
    					}
    					
    				/*	else if(i == 0 && tempWord[0].equals("y"))
    					{
    						tempSubstring = s.substring(0,1);				 //getting last part of array before vowel
    						tempSubstring2 = s.substring(1);	
    					
    						newWord = tempSubstring2 + tempSubstring + "ay";
    						
    						if(wasUCase == 1)   																			//if first letter uppercase
    						{
    							newWordCaps = newWord.substring(0,1);   										  //gets first letter of new word
    							newWordCaps = newWord.replace(newWordCaps, newWordCaps.toUpperCase()); //replaces first letter of new word with upperCase letter
    						}
    					}
    					
    					
    					/*else if(strTempLetter.equals("y"))  //if y then it will cut the array and send it back
    					{
    						tempSubstring = s.substring(0,indexOfLetter); //getting last part of array before vowel
    						tempSubstring2 = s.substring(indexOfLetter));
    					
    						newWord = tempSubstring2 + tempSubstring + "ay"; 		
    					}*/
    					
    					
    					
    					else	//normal change
    					{
    						
    						tempSubstring = s.substring(0,indexOfLetter);				 //getting first part of array before vowel
    						tempSubstring2 = s.substring(indexOfLetter);		
    						newWord = tempSubstring2 + tempSubstring + "ay";     
    						
    						String[] newWordArray = new String[newWord.length()]; //new array to look through new word
    						
    						for(int k = 0; k < newWord.length(); k++)     //scans new array
    						{
    							newerLetter = newWordArray[k];					//letter of index goes into newLetter
    							int indexLetter = k;									//getting current index
    							
    							if(newerLetter.equals("?")||newerLetter.equals("!")||newerLetter.equals(".")) //looking for punc
    							{
    								tempPunc = newerLetter.substring(k, k+1); 				//takes punc into temp
    								newWord = newWord.replace(newerLetter, "");				//deletes punc
    								newWord = newWord + tempPunc;									//adds punc at the end of newWord 
    								if(newWordArray[k+1].equals("\""))							//looking if quotes are after punc - "sunny!"
    								{
    									tempPunc2 = newerLetter.substring(k+1, k+2); 		//moves quotes after punc
    									newWord = newWord.replace(newerLetter, "");
    									newWord = newWord + tempPunc2;
    								}
    							}
    							else if(newerLetter.equals("\""))							//checks if " is in the middle, and not directly after a punc
    							{
    								String beforeQ;
    								String afterQ;
    								String tempQ;
    								
    								tempQ = newerLetter.substring(k, k+1); 				//takes punc into temp
    								newWord = newWord.replace(newerLetter, "");			//deletes punc
    								newWord = tempQ + newWord;	    							//adds " to beginning
    							}
    						} 					
    					}
    				}				
    			}
    		return newWord;     
          }
    		
    	
          public static void go() throws FileNotFoundException
          {
             /***************************
             *
             *   enter your code here:
          		 Scanner class, try-catch, nested loops, file output  
             *
             ****************************/
    			
    			
    			Scanner x = new Scanner(System.in);
    			
    			System.out.print("Enter the file name: ");
    			String fileName = x.nextLine();
    			String e = "";
    			Scanner inFile = new Scanner(new File(fileName));
    			
    			while(inFile.hasNext())
    			{
    				e = pig(inFile.next());	
    			}	
    			
    			inFile.nextLine();
    	 
    			try
    			{
    			String fileOut = "processed.txt";
    			PrintStream outFile = new PrintStream(new FileOutputStream(fileOut));
    			outFile.close();
    			}
    			catch(NullPointerException q)
    			{
    				System.out.println("Unable to creat " + fileName + ": " + q.getMessage());
    			}
          }
       }
    Last edited by pbrockway2; 10-18-2012 at 01:12 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Pig Latin translator

    Hi zygmfb, welcome to the forums!

    I have added "code" tags to your post. The idea is that you put [code] at the start of any code and [/code] at the end: that way the formatting is preserved by the forum software and the code is readable. It is also a good idea to use spaces rather than tabs to indent code as the latter can be rather wide when rendered by a web browser.

    To begin figuring out why you get a null pointer exception we'll have to be clear about where this exception is occurring. Post the entire stack trace you get (the thing that starts by reporting a NullPointerException and then follows with a number of lines saying which lines of code were involved.)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Pig Latin translator

    You should also say what you expect the go() method to do. That is the step-by-step procedure you intend it to carry out.

    I mention this because some of the odd here looks a little odd.

    Java Code:
    while(inFile.hasNext())
    {
        e = pig(inFile.next());	
    }
    This code repeatedly reads a character from the scanner, and then assigns it to e discarding the value that was previously assigned to e. The overall effect is for e to end up a a string consisting of the very last thing the scanner read all the rest of the input being discarded.

    Java Code:
    PrintStream outFile = new PrintStream(new FileOutputStream(fileOut));
    outFile.close();
    This code creates an instance of PrintStream and then immediately closes it without writing any output.

  4. #4
    zygmfb is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Pig Latin translator

    Ok thank you for letting me know, this is my first post I appreciate it.

    Java Code:
    // Sunny Eltepu, 10/17/12
       import java.util.*;
       import java.io.*;
       import java.lang.*;
       public class PigLatin_shell
       {
          public static void main(String[] args) throws FileNotFoundException
          {
             go();
             
          }
          public static String pig(String s)
          {
             String strTempLetter = ""; //letter transfer from norm. array
             String newerLetter = "";   //letter transfer for punc. array
          	
             String tempSubstring = "";
             String tempSubstring2 = ""; //substrings for both sides
             String tempPunc = "";
             String tempPunc2 = "";
          	
             String newWordCaps = "";
          	
             int wasUCase = 0;
             int yWasFirst = 0;
          	
             int indexOfLetter;
             String newWord = "";
             String [] tempWord = new String[s.length()]; //makes s letters into array
          	
          	
          	
             for(int i = 0; i < s.length(); i++)
             {				
                strTempLetter = tempWord[i];		//.toLowerCase() //putting element letter into String and changes letter to lowercase
                String uCaseCheck = "";
                if(i == 0)
                {
                   uCaseCheck = strTempLetter.toUpperCase(); //making a fake var uppercase to see if normal is upperCase
                   if(tempWord[0].equals(uCaseCheck))			 //returns true if first letter is upper case, makes it lowerCase
                   {
                      wasUCase = 1;		
                      strTempLetter = tempWord[0].toLowerCase();  //makes first letter lowerCase so it can run through program
                   	
                      if(tempWord[0].equals("y"))
                         yWasFirst = 1;
                   }
                }	
             																			//strTempLetter = strTempLetter.toLowerCase();
                indexOfLetter = i; 				//getting index of the letter
             	
             	
                if(strTempLetter.equals("a") || strTempLetter.equals("e") || strTempLetter.equals("i") || strTempLetter.equals("o") || strTempLetter.equals("u") || ((strTempLetter.equals("y") && !(yWasFirst == 1)))) //BASIC RULES AND Y
                {
                	
                   if(strTempLetter.equals("u") && tempWord[indexOfLetter - 1].equals("q")) //SPECIAL checks if q is before u
                   {
                      tempSubstring = s.substring(0,indexOfLetter); 								 //getting last part of array before including u and q
                      tempSubstring2 = s.substring(indexOfLetter); 		 						 //first part of word before u, excluding u (which sends u to back)
                   
                      newWord = tempSubstring2 + tempSubstring + "ay";
                   	
                      if(wasUCase == 1)   																			//if first letter uppercase
                      {
                         newWordCaps = newWord.substring(0,1);   										  //gets first letter of new word
                         newWordCaps = newWord.replace(newWordCaps, newWordCaps.toUpperCase()); //replaces first letter of new word with upperCase letter
                      }
                   		
                   }
                   
                   	else if(i == 0 && tempWord[0].equals("y"))
                   {
                   	tempSubstring = s.substring(0,1);				 //getting last part of array before vowel
                   	tempSubstring2 = s.substring(1);	
                   
                   	newWord = tempSubstring2 + tempSubstring + "ay";
                   	
                   	if(wasUCase == 1)   																			//if first letter uppercase
                   	{
                   		newWordCaps = newWord.substring(0,1);   										  //gets first letter of new word
                   		newWordCaps = newWord.replace(newWordCaps, newWordCaps.toUpperCase()); //replaces first letter of new word with upperCase letter
                   	}
                   }
                   
                   
                   else if(strTempLetter.equals("y"))  //if y then it will cut the array and send it back
                   {
                   	tempSubstring = s.substring(0,indexOfLetter); //getting last part of array before vowel
                   	tempSubstring2 = s.substring(indexOfLetter));
                   
                   	newWord = tempSubstring2 + tempSubstring + "ay"; 		
                   }
                   
                   
                   
                   else	//normal change
                   {
                   	
                      tempSubstring = s.substring(0,indexOfLetter);				 //getting first part of array before vowel
                      tempSubstring2 = s.substring(indexOfLetter);		
                      newWord = tempSubstring2 + tempSubstring + "ay";     
                   	
                      String[] newWordArray = new String[newWord.length()]; //new array to look through new word
                   	
                      for(int k = 0; k < newWord.length(); k++)     //scans new array
                      {
                         newerLetter = newWordArray[k];					//letter of index goes into newLetter
                         int indexLetter = k;									//getting current index
                      	
                         if(newerLetter.equals("?")||newerLetter.equals("!")||newerLetter.equals(".")) //looking for punc
                         {
                            tempPunc = newerLetter.substring(k, k+1); 				//takes punc into temp
                            newWord = newWord.replace(newerLetter, "");				//deletes punc
                            newWord = newWord + tempPunc;									//adds punc at the end of newWord 
                            if(newWordArray[k+1].equals("\""))							//looking if quotes are after punc - "sunny!"
                            {
                               tempPunc2 = newerLetter.substring(k+1, k+2); 		//moves quotes after punc
                               newWord = newWord.replace(newerLetter, "");
                               newWord = newWord + tempPunc2;
                            }
                         }
                         else if(newerLetter.equals("\""))							//checks if " is in the middle, and not directly after a punc
                         {
                            String beforeQ;
                            String afterQ;
                            String tempQ;
                         	
                            tempQ = newerLetter.substring(k, k+1); 				//takes punc into temp
                            newWord = newWord.replace(newerLetter, "");			//deletes punc
                            newWord = tempQ + newWord;	    							//adds " to beginning
                         }
                      } 					
                   }
                }				
             }
             return newWord;     
          }
       	
       
          public static void go() throws FileNotFoundException
          {
             /***************************
             *
             *   enter your code here:
          		 Scanner class, try-catch, nested loops, file output  
             *
             ****************************/
          	
          	
             Scanner x = new Scanner(System.in);
          	
             System.out.print("Enter the file name: ");
             String fileName = x.nextLine();
             String e = "";
             Scanner inFile = new Scanner(new File(fileName));
          	
             while(inFile.hasNext())
             {
                e = pig(inFile.next());	
             }	
          	
             inFile.nextLine();
          
             try
             {
                String fileOut = "processed.txt";
                PrintStream outFile = new PrintStream(new FileOutputStream(fileOut));
                outFile.close();
             }
                catch(NullPointerException q)
                {
                   System.out.println("Unable to creat " + fileName + ": " + q.getMessage());
                }
          }
       }
    OUTPUT:

    ----jGRASP exec: java PigLatin_shell

    Enter the file name: words.txt
    Exception in thread "main" java.lang.NullPointerException
    at PigLatin_shell.pig(PigLatin_shell.java:39)
    at PigLatin_shell.go(PigLatin_shell.java:159)
    at PigLatin_shell.main(PigLatin_shell.java:9)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    the go() method is supposed to take the name of a file, change the words to pig latin, and then make a new file named "processed.txt"

    I don't exactly know how to write the go() method, so can please you help me on that too?
    Last edited by zygmfb; 10-18-2012 at 02:30 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Pig Latin translator

    I strongly suggest you work on one thing at a time. One method at a time: make sure you know what the method is supposed to do (the aim) and how (step by step by step) you are intending to achieve that aim. Then test what you have written.

    The pig() method would a good place to start.

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    at PigLatin_shell.pig(PigLatin_shell.java:39)
    at PigLatin_shell.go(PigLatin_shell.java:159)
    at PigLatin_shell.main(PigLatin_shell.java:9)
    You read these stack traces from the top down to where it mentions your code. In this case the null pointer exception is occuring on line 39 in the pig() method.

    Java Code:
    uCaseCheck = strTempLetter.toUpperCase();
    You will get a NullPointerException when you use a variable as if it had a null value when it is really null. In this case it looks like strTempLetter is null. And you can't call a method like toUpperCase() on something that is null.

    You can check whether strTempLetter is null by using System.out.println().

    Java Code:
    if(i == 0)
    {
        System.out.println("About to assign to uCheckCase, strTempLetter=" + strTempLetter);
        uCaseCheck = strTempLetter.toUpperCase(); //making a fake var uppercase to see if normal is upperCase
        if(tempWord[0].equals(uCaseCheck))            //returns true if first letter is upper case, makes it lowerCase
        {
            // etc
    If it turns out that strTempLetter is null, go back through the code (you don't have to go back far!) to where you thought you had assigned a nonnull value to strTempLetter and try to figure out why that didn't happen.

Similar Threads

  1. english to pig latin, I/O in gui
    By MaceMan in forum New To Java
    Replies: 18
    Last Post: 11-13-2011, 04:53 AM
  2. English to Binary Translator (Please help)
    By Baconator in forum New To Java
    Replies: 10
    Last Post: 10-01-2011, 10:15 PM
  3. Pig Latin Translator
    By danthegreat in forum New To Java
    Replies: 22
    Last Post: 09-10-2011, 11:42 PM
  4. Custom Font Translator
    By Nerdopolis in forum New To Java
    Replies: 3
    Last Post: 04-18-2009, 03:50 AM
  5. Translator hashtable
    By editor35 in forum New To Java
    Replies: 1
    Last Post: 01-11-2009, 02:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •