Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Making a Translator program for Pig Latin


  • This topic is locked This topic is locked
14 replies to this topic

#1 sidd0123

sidd0123

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 14 April 2010 - 02:04 PM

I'll try to use the right terminology as I explain what this project is about. We are required to make a translator program using Java. My teachers main will take care of the input string, which is what the user wants translated (The language is Pig Latin. I'll post the rules below as well.) We need to create 5 different, as my teacher put it "constant values as integers to be used as translate options".

Here they are:

o UNCHANGED do nothing
o LOWERCASE translate the string to lower case
o UPPERCASE TRANSLATE THE STRING TO UPPER CASE
o ALTERNATE TrAnSlAtE AlTeRnAtInG LeTtErS To UpPeR/LoWeR CaSe
o PIGLATIN anslatetray ethay ingstray otay igpay atinlay


Rules of Pig Latin:
In words that begin with consonant sounds, the initial consonant or consonant cluster is moved to the end of
the word, and "ay" is added to the end of the word.
In words that begin with vowel sounds or silent consonants, the syllable "ay" is added to the end of the
word.


I have two methods. One is a getAuthor method, which I have taken care of, and the other is public static String Xlate(int, String). The int parameter is which translate options to use from the above list in bold. Those tranlate options are in an integer array assigned a position from [0] to [4]. So if I pick [0] it will leave it as it is and return it back to me. The String parameter is the String that the user enters into the main to be translatedThese are just details so I can properly ask my question.

Here is my code, which I haven't gotten far in at all yet:

package package10;public class Xlator {	public static int UNCHANGED;	public static int LOWERCASE;	public static int UPPERCASE;	public static int ALTERNATE;	public static int PIGLATIN;	public static String Xlate(int i, String input) {												return input;	}}



Here is my Main:

int [] translator = {
Xlator.UNCHANGED,
Xlator.LOWERCASE,
Xlator.UPPERCASE,
Xlator.ALTERNATE,
Xlator.PIGLATIN,
// Xlator.SWEDISHCHEF
};

String input = JOptionPane.showInputDialog(null,"Enter the text for translation _linenums:0'>package package10;import javax.swing.JOptionPane;public class XlatorMain { /** * @param args */ public static void main(String[] args) { // if you don't have one (or more) of these translators implemented, jut comment out the line int [] translator = { Xlator.UNCHANGED, Xlator.LOWERCASE, Xlator.UPPERCASE, Xlator.ALTERNATE, Xlator.PIGLATIN, // Xlator.SWEDISHCHEF }; String input = JOptionPane.showInputDialog(null,"Enter the text for translation: "); while (input!= null) { String outputString = ""; for (int j = 0; j < translator.length; j++) { String ans1 = Xlator.Xlate(translator[j], input); outputString = outputString + "\n'" + input + "' translates to '" + ans1 + "' using method " + translator[j]; } input = JOptionPane.showInputDialog(null,outputString + "\n\nEnter the text for translation: "); } }}

My question is, how would I tell my program that this code is represents the UNCHANGED option, and so forth?

Visit http://central.wmrhsd.org/FACULTY_FILES/ah...0Translator.pdf if my explanation makes you more confused.

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 14 April 2010 - 03:34 PM

It tells you right in your instructions what parameters the Xlate method takes. One of them is an integer. You read the integer, and decide what to do. Use either a switch, or an if-if else block, ie:
if(i=0) {  //upper case
   //code to convert to upper case
} else if (i=1) {  //convert to pig latin
  //code to convert to pig latin
} ...

Pretty straight forward.

#3 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 18 April 2010 - 12:57 PM

Sorry about that, don't know why I didn't connect that.

#4 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 18 April 2010 - 03:02 PM

I'm stuck again. I was pondering on this for a while.

I'll post my updated code here. Just a sec.
package package10;public class Xlator {		public static final int UNCHANGED = 0;	public static final int LOWERCASE = 1;	public static final int UPPERCASE = 2;	public static final int ALTERNATE = 3;	public static final int PIGLATIN = 4;			public static String getAuthor(){				return "Sid Patel";	}		public static String Xlate(int translator, String sentence){				if (translator == 0){						return sentence;		}				else if (translator == 1){						return sentence.toLowerCase();		}				else if (translator == 2){						return sentence.toUpperCase();		}				else if (translator == 3){						sentence.toUpperCase();						char[] letterArray = sentence.toCharArray();						for (int i = 1; i <= letterArray.length; i = i + 2){							letterArray[i].toLowerCase();// Can't do this ERROR TYPE: (<strong class='bbc'>Cannot invoke toLowerCase() on the primitive type char</strong>)							}												return sentence;		}				else 			return sentence;		}	}


I believe it is saying that it can't apply the LowerCase method on a single character in an array. This is the ALTERNATING METHOD by the way. Any suggestions? I plan on trying a word array, maybe that might work?

Is there anyway that I can change the case of just one letter in the string rather than the whole thing?

Edited by sidd0123, 18 April 2010 - 03:19 PM.


#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 18 April 2010 - 07:45 PM

First, there is no need to declare any variables regarding how integers map to functions.

You are not using the 'toLowerCase' method properly.
http://java.sun.com/j2se/1.4.2/docs/api/ja.../Character.html

EDIT: What do you mean by a 'word' array? Have you created a 'word' data type?

#6 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 18 April 2010 - 08:06 PM

I'm not sure what "integers map to functions" to means.

You are not using the 'toLowerCase' method properly.


I think I fixed this problem, or made it a little better.


EDIT: What do you mean by a 'word' array? Have you created a 'word' data type?


This was just a thought, that didn't quite work out.

Updated Code:

sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1);


}
return sentence;

}

else
return sentence;
}

}
// Just links that i found helpful
//http _linenums:0'>package package10;public class Xlator { public static final int UNCHANGED = 0; public static final int LOWERCASE = 1; public static final int UPPERCASE = 2; public static final int ALTERNATE = 3; public static final int PIGLATIN = 4; public static String getAuthor(){ return "Sid Patel"; } public static String Xlate(int translator, String sentence){ if (translator == 1){ return sentence.toLowerCase(); } else if (translator == 2){ return sentence.toUpperCase(); } else if (translator == 3){ sentence = sentence.toLowerCase(); for (int i = 0; i <= sentence.length(); i = i + 2){ <strong class='bbc'>sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1);</strong> } return sentence; } else return sentence; } } // Just links that i found helpful//http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int, int) //http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#charAt(int) //http://www.daniweb.com/forums/thread108555.html //http://central.wmrhsd.org/FACULTY_FILES/aherzog/1477/Assignments/CS%20-%20Assignment%2010%20-%20Translator.pdf

I was wondering if you could help me with the bold line. I had some other ideas, but found them a little too complicated and time consuming, until I did a little bit of google searching and found the bold part. It's a piece of code I found on one of the websites above. I didn't understand it at first, but here is what I do know:


sentence = sentence.toLowerCase();

This will take my inputed string and change all of the characters in the string to lower case.




for (int i = 0; i <= sentence.length(); i = i + 2){


This will start a loop. The i stands for which character to select. It starts at 0, can't be more than the string's length, and will increase by 2.

sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1);

In this piece of code, I would like it to do the following: For example, String sentence = "Hello".

HeLlO

This is what it currently prints out:
Oello


I'm not saving the first few changes right?

Edited by sidd0123, 18 April 2010 - 08:13 PM.


#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 18 April 2010 - 09:06 PM

Let's try this again. This code here:
public static final int UNCHANGED = 0;
public static final int LOWERCASE = 1;
public static final int UPPERCASE = 2;
public static final int ALTERNATE = 3;
public static final int PIGLATIN = 4;
..is completely unnecessary. Where are you going to use it?

Did you look at the link I gave you? It tells you exactly how to change a char to upper or lower case:

toLowerCase(char ch)

The API is telling you that if you provide a char to the method 'toLowerCase', it will return the char you provide as lower case (or upper case, depending on what you want to do).This here:
sentence = sentence.toLowerCase();
..is for converting a String to lower case. A String and a char are not the same thing.

This is a bad idea.
for (int i = 0; i <= sentence.length(); i = i + 2)
You are assuming that letters are all going to be lower case, or all upper case. Maybe you should consider what you should do if i is even, or if i is odd. Also, this is going to fall off the end of your array and generate an error.

#8 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 18 April 2010 - 09:36 PM

[codebox]public static final int UNCHANGED = 0;
public static final int LOWERCASE = 1;
public static final int UPPERCASE = 2;
public static final int ALTERNATE = 3;
public static final int PIGLATIN = 4;[/codebox]

I can't remove this because my then my main gives me an error. The error says (Xlator.PIGLATIN cannot be resolved).

sentence = sentence.toLowerCase();
I had this because if I had "Hello" and my code said start by making the first letter capital, it wouldn't run into an error. (Is this irrelevant code?)

Are there any counters that implement an even or odd, or would I have to resort to loops. I was unable to find any such methods on Java Docs.

#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 18 April 2010 - 09:49 PM

The error says (Xlator.PIGLATIN cannot be resolved

I don't know what the rest of your code looks like, but I promise that if removing none of the others caused an error, then that shouldn't either.

sentence = sentence.toLowerCase();
I had this because if I had "Hello" and my code said start by making the first letter capital, it wouldn't run into an error. (Is this irrelevant code?)

And that is exactly how you would turn an entire string to lower case. A char is not a string, and thus has to be handled differently. I showed you how.

Are there any counters that implement an even or odd, or would I have to resort to loops.

You're kidding, right? How can you tell if a number is even or odd? You are not even trying.

#10 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 18 April 2010 - 10:20 PM

The error says (Xlator.PIGLATIN cannot be resolved)

It happens to all of them. Was using the above as an example.

I deleted sentence = sentence.toLowerCase(); and use the link you gave me and came up with this:

sentence = Character.toUpperCase(sentence.charAt(0)) + sentence.substring(1);

When I input "hello" it outputs "Hello". This is exactly what i wold like to do, but also make the 3rd and 5th characters Upper case as well.

Are there any counters that implement an even or odd, or would I have to resort to loops.



Sorry, this was a typo. I meant are their any methods that count using even or odd integers?

Edited by sidd0123, 18 April 2010 - 10:20 PM.


#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 18 April 2010 - 11:05 PM

meant are their any methods

2nd grade math....

#12 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 19 April 2010 - 02:17 PM

Every time I try a loop with an even counter, it doesn't work. I believe that the problem is that I am not saving my changes and so each time they are overwriting.

Is there a solution to this?

#13 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 19 April 2010 - 03:57 PM

Am I just supposed to guess what your code looks like?

I am not going to do your assignment for you. I ma not going to give you code. Show me what you have, and I can tell you what you are doing wrong. "It doesn't work" doesn't tell me anything at all.

#14 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:40 PM

Posted 19 April 2010 - 05:32 PM

I think I my have found a solution. Sorry, I forgot about the code.
[codebox]package package10;

public class Xlator {

public static final int UNCHANGED = 0;
public static final int LOWERCASE = 1;
public static final int UPPERCASE = 2;
public static final int ALTERNATE = 3;
public static final int PIGLATIN = 4;


public static String getAuthor(){

return "Sid Patel";
}

public static String Xlate(int translator, String sentence){



if (translator == 1){

return sentence.toLowerCase();
}

else if (translator == 2){

return sentence.toUpperCase();
}

else if (translator == 3){


for (int i = 0 ; i < sentence.length () ; i++)
{
if (i % 2 == 0)
{
sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
}
}
return sentence;
}

else if (translator == 4){
//working on code
return sentence;


}
else
return sentence;
}
}

//http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int, int)
//http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#charAt(int)
//http://www.daniweb.com/forums/thread108555.html
[/codebox]


My main is in the starting post.

#15 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:40 PM

Posted 19 April 2010 - 05:58 PM

Good luck with your project. I have better things to do than guess what problems you are having. "It doesn't work" could mean anything from "my code crashes the computer" to "I am too lazy to do any research on my own."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users