In my code, I am creating a window where a user can input a phrase. I am then setting up a for loop to read through the input and add ub before any vowel. My problem now is that if the user input is aeiou the output I want is ubaeiou and not ubaubeubiuboubu. I believe a Boolean variable would help but I am stuck on how to do this portion.
public void buttonPressed() {
String line = input1.getText();
String finline;
finline = "";
line = line.toLowerCase();
for(int i =0; i < line.length(); i++) {
if((line.charAt(i) == 'a') || (line.charAt(i) == 'e') || (line.charAt(i) == 'i') || (line.charAt(i) == 'o') || (line.charAt(i) == 'u'))
{
finline = finline + "ub" + line.charAt(i);
}
else
{
finline = finline + line.charAt(i);
}
}
output.setText(finline);
}
User input = aeiou
Output = ubaubeubiuboubu
Desired output = ubaeiou
You want the "ub" prefix on the first vowel of the LINE? Then use a boolean to trigger an early loop exit.
public void buttonPressed() {
Boolean done = false;
String line = input1.getText();
String finline;
finline = "";
line = line.toLowerCase();
for(int i =0; i < line.length() && !done; i++) {
if((line.charAt(i) == 'a') || (line.charAt(i) == 'e') || (line.charAt(i) == 'i') || (line.charAt(i) == 'o') || (line.charAt(i) == 'u'))
{
finline = finline + "ub" + line.charAt(i);
done = true;
}
else
{
finline = finline + line.charAt(i);
}
}
output.setText(finline);
}
Or if you want it on the first vowel of EACH WORD, you'll need to work the boolean into the vowel check condition and make sure it gets turned off on word breaks.
public void buttonPressed() {
Boolean checkVowel = true;
String line = input1.getText();
String finline;
finline = "";
line = line.toLowerCase();
for(int i =0; i < line.length(); i++) {
if(checkVowel && ((line.charAt(i) == 'a') || (line.charAt(i) == 'e') || (line.charAt(i) == 'i') || (line.charAt(i) == 'o') || (line.charAt(i) == 'u')))
{
finline = finline + "ub" + line.charAt(i);
checkVowel = false;
}
else
{
finline = finline + line.charAt(i);
if (line.charAt(i) == ' ') checkVowel = true;
}
}
output.setText(finline);
}
try this:
String line = "aeiou";
String finline;
finline = "";
line = line.toLowerCase();
Boolean flag = true;
for (int i = 0; i < line.length(); i++) {
Boolean flagLine = (line.charAt(i) == 'a') || (line.charAt(i) == 'e') || (line.charAt(i) == 'i') || (line.charAt(i) == 'o')
|| (line.charAt(i) == 'u');
if (flag && flagLine) {
finline = finline + "ub" + String.valueOf(line.charAt(i));
flag = false;
}
else {
if(!flagLine) flag = true;
finline = finline + String.valueOf(line.charAt(i));
}
}
System.out.println(finline);
input = aeiou
output = ubaeiou
input = aeqou
output = ubaequbou
Related
here is a logical error how can i find the most vowel sentence has the user entered .
import java.util.Scanner;
public class ZanoreShqip {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String fjalia;
int count_zanore = 0;
int maxVowelCount = 0;
String wordWithMostVowels = "";
final String SENTINEL = "FUND";
System.out.print("Sheno fjali dhe fund per te perfunduar : ");
fjalia=input.nextLine();
while(!fjalia.equalsIgnoreCase(SENTINEL)) {
fjalia = fjalia.toLowerCase();
for(int i = 0; i <fjalia.length(); ++i) {
char ch = fjalia.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
count_zanore++;
} if ( count_zanore > maxVowelCount) {
wordWithMostVowels = fjalia;
maxVowelCount = count_zanore;
}
}
System.out.print("\nSheno fjali dhe fund per te perfunduar : ");
fjalia = input.nextLine();
}
System.out.println(wordWithMostVowels);
System.out.println(maxVowelCount);
}
}
You forgot this part count_zanore = 0;
Try this
while(!fjalia.equalsIgnoreCase(SENTINEL)){
fjalia = fjalia.toLowerCase();
for(int i = 0; i <fjalia.length(); ++i)
{
char ch = fjalia.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u'){
count_zanore++;
}
if ( count_zanore > maxVowelCount){
wordWithMostVowels = fjalia;
maxVowelCount = count_zanore;
}
}
count_zanore = 0;
System.out.print("\nSheno fjali dhe fund per te perfunduar : ");
fjalia = input.nextLine();
}
This is my first question here so I'm sorry for any mistake. I am writing a tic-tac-toe game. I have problem with this.
if (!table[0][0].equals(" ") && !table[1][0].equals(" ") && !table[2][0].equals(" ") &&
!table[0][1].equals(" ") && !table[1][1].equals(" ") && !table[2][1].equals(" ") &&
!table[0][2].equals(" ") && !table[1][2].equals(" ") && !table[2][2].equals(" ")) {
System.out.println("Draw!");
return true;
}
For example: if I insert any String in table[0][0], it returns true despite other fields being " ".
I have initialized table in Board class. This is the constructor:
public Board() {
sc = new Scanner(System.in);
board = new Mark[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = new Mark(" ");
}
}
}
This is my checkResult method:
public boolean checkResult() {
if ((table[0][0] == table[1][0] && table[1][0] == table[2][0] && !table[2][0].equals(" "))
|| (table[0][1] == table[1][1] && table[1][1] == table[2][1] && !table[2][1].equals(" "))
|| (table[0][2] == table[1][2] && table[1][2] == table[2][2] && !table[2][2].equals(" "))
|| (table[0][0] == table[0][1] && table[0][1] == table[0][2] && !table[0][2].equals(" "))
|| (table[1][0] == table[1][1] && table[1][1] == table[1][2] && !table[1][2].equals(" "))
|| (table[2][0] == table[2][1] && table[2][1] == table[2][2] && !table[2][2].equals(" "))
|| (table[0][0] == table[1][1] && table[1][1] == table[2][2] && !table[2][2].equals(" "))
|| (table[0][2] == table[1][1] && table[1][1] == table[2][0] && !table[2][0].equals(" "))) {
System.out.println("You won");
return true;
}
if (!table[0][0].equals(" ") && !table[1][0].equals(" ") && !table[2][0].equals(" ") && !table[0][1].equals(" ")
&& !table[1][1].equals(" ") && !table[2][1].equals(" ") && !table[0][2].equals(" ")
&& !table[1][2].equals(" ") && !table[2][2].equals(" ")) {
System.out.println("Draw!");
return true;
}
return false;
}
And this is my Test class:
public class Test {
public static void main(String[] args) {
Logika lg = new Logika();
do{
lg.insertMark2();
lg.printBoard();
} while(!(lg.checkResult()));
}
}
When I do my first insert, program stops.
Basically have a simple process that involves mapping letters and numbers 2 spaces forward so an A would encode to C , 1 to 3 and such. There is wraparound for so Z would encode to B and 0 would encode to 2. The encode works fine, but after the first cycle of the loop the decode seems to map back 4 spaces instead of 2. Here's the code, let me know what you guys think the problem is.
Encode/Decode
public class EncodeDecode {
private String[] originalList;
private String[] encodedList;
private String[] decodedList;
public EncodeDecode(String[] oL) {
originalList = oL;
encodedList = originalList;
decodedList = originalList;
for (int cnt = 0; cnt < oL.length; cnt++) {
encodedList[cnt] = originalList[cnt];
}
for (int cnt = 0; cnt < oL.length; cnt++) {
decodedList[cnt] = originalList[cnt];
}
}
public String encode(String originalWord) {
//Maps every character in original word to 2 positions forward w/ wrap around.
String result = "";
for (int cnt = 0; cnt < originalWord.length(); cnt++) {
result += forwardMap(originalWord.charAt(cnt));
}
encodedList[0] = result;
return result;
}
public String decode(String codedWord) {
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++) {
result += backMap(codedWord.charAt(cnt));
}
decodedList[0] = result;
return result;
}
public char forwardMap(char ch) {
char result = Character.toLowerCase(ch);
if (result == 'a') return 'c';
if (result == 'b') return 'd';
if (result == 'c') return 'e';
if (result == 'd') return 'f';
if (result == 'e') return 'g';
if (result == 'f') return 'h';
if (result == 'g') return 'i';
if (result == 'h') return 'j';
if (result == 'i') return 'k';
if (result == 'j') return 'l';
if (result == 'k') return 'm';
if (result == 'l') return 'n';
if (result == 'm') return 'o';
if (result == 'n') return 'p';
if (result == 'o') return 'q';
if (result == 'p') return 'r';
if (result == 'q') return 's';
if (result == 'r') return 't';
if (result == 's') return 'u';
if (result == 't') return 'v';
if (result == 'u') return 'w';
if (result == 'v') return 'x';
if (result == 'w') return 'y';
if (result == 'x') return 'z';
if (result == 'y') return 'a';
if (result == 'z') return 'b';
if (result == '0') return '2';
if (result == '1') return '3';
if (result == '2') return '4';
if (result == '3') return '5';
if (result == '4') return '6';
if (result == '5') return '7';
if (result == '6') return '8';
if (result == '7') return '9';
if (result == '8') return '0';
if (result == '9') return '1';
if (result == ' ')
return ' ';
else
return '$';
}
public char backMap(char ch) {
char result = Character.toLowerCase(ch);
if (result == 'c') return 'a';
if (result == 'd') return 'b';
if (result == 'e') return 'c';
if (result == 'f') return 'd';
if (result == 'g') return 'e';
if (result == 'h') return 'f';
if (result == 'i') return 'g';
if (result == 'j') return 'h';
if (result == 'k') return 'i';
if (result == 'l') return 'j';
if (result == 'm') return 'k';
if (result == 'n') return 'l';
if (result == 'o') return 'm';
if (result == 'p') return 'n';
if (result == 'q') return 'o';
if (result == 'r') return 'p';
if (result == 's') return 'q';
if (result == 't') return 'r';
if (result == 'u') return 's';
if (result == 'v') return 't';
if (result == 'w') return 'u';
if (result == 'x') return 'v';
if (result == 'y') return 'w';
if (result == 'z') return 'x';
if (result == 'a') return 'y';
if (result == 'b') return 'z';
if (result == '2') return '0';
if (result == '3') return '1';
if (result == '4') return '2';
if (result == '5') return '3';
if (result == '6') return '4';
if (result == '7') return '5';
if (result == '8') return '6';
if (result == '9') return '7';
if (result == '0') return '8';
if (result == '1') return '9';
if (result == ' ')
return ' ';
else
return '$';
}
public String[] getEncodedList() {
return encodedList;
}
public String[] getDecodedList() {
return decodedList;
}
}
Tester
public class EncodeDecodeTester {
public static void main(String[] args) {
String[] list = {"abcd", "abcd", "abcd"};
EncodeDecode ec = new EncodeDecode(list);
String[] encode = ec.getEncodedList();
for (int index = 0; index < encode.length; index++) {
System.out.println("Encoded: " +ec.encode(list[index]));
System.out.println("Decoded: " +ec.decode(list[index]));
}
}
}
The first thing I notice is object references on String[], change this
encodedList = originalList;
decodedList = originalList;
to
encodedList = new String[originalList.length];
decodedList = new String[originalList.length];
NOtice that in your main function you are calling a decode on the actual string and not on the encoded string. so your program is working properly if the initializations are correct as Elliot pointed.
for (int index = 0; index < encode.length; index++)
{
System.out.println("Encoded: " +ec.encode(list[index]));
System.out.println("Decoded: " +ec.decode(ec.encode(list[index])));
}
and that will give you your desired output
Ending up finding the answer after enough tinkering around with things and bouncing ideas off my roommates.
Changed this:
public String decode(String codedWord)
{
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++)
{
result += backMap(codedWord.charAt(cnt));
}
decodedList[0] = result;
return result;
}
To this:
public String decode(String codedWord)
{
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++)
{
result += backMap(encodedList[0].charAt(cnt));
}
decodedList[0] = result;
return result;
}
I'd like some help with a problem I have in the following code.
package piglatin;
public class asdg {
public static void main(String[] args) {
String word = "fifteen";
int vowelSpot = findFirstVowelPosition(word);
String wordBeg = "";
String wordEnd = "ay";
String wordNew = "";
System.out.println(vowelSpot);
wordBeg = word.substring(0,vowelSpot-1);
System.out.println(wordBeg);
word = word.replace(wordBeg,"");
System.out.println(word);
wordNew = word + wordBeg + wordEnd;
System.out.println(wordNew);
}
public static int findFirstVowelPosition(String word)
{
int vowelPosition = -1;
word = word.trim();
for(int i=1; i <=word.length(); i++)
{
if ((word.charAt(i-1) == 'a') ||(word.charAt(i-1) == 'A')
||(word.charAt(i-1) == 'e') ||(word.charAt(i-1) == 'E')
||(word.charAt(i-1) == 'i') ||(word.charAt(i-1) == 'I')
||(word.charAt(i-1) == 'o') ||(word.charAt(i-1) == 'O')
||(word.charAt(i-1) == 'u') ||(word.charAt(i-1) == 'U')
||(word.charAt(i-1) == 'y') ||(word.charAt(i-1) == 'Y')){
vowelPosition = i;
return vowelPosition;
}
}
return vowelPosition;
}
}
The purpose of this code is to take the string Word and put it in piglatin. Word will start with a consonant. The problem I'm having is when the string contains a specific consonant, then the first vowel, and the same consonant, it'll get rid of the second consonant.
This specific code isn't for homework, however I'll be applying this to a homework assignment.
I've got it:
Replace this line:
word = word.replace(wordBeg,"");
with
word = word.replaceFirst(wordBeg,"");
Check the documentation on it: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
The method takes in any name and tests whether a character is a vowel or consonant. If it's a vowel, it makes the character uppercase, if it's a consonant, it makes the character lowercase. Any thoughts? I don't know how to add .toUpperCase and .toLowerCase in the if else statements.
public static void parsing(String name[])
{
String temp = name[0];
int i = 0;
for(i = 0; i < temp.length(); i++)
{
if(temp.charAt(i) == 'a' || temp.charAt(i) == 'A' ||
temp.charAt(i) == 'e' || temp.charAt(i) == 'E' ||
temp.charAt(i) == 'i' || temp.charAt(i) == 'I' ||
temp.charAt(i) == 'o' || temp.charAt(i) == 'O' ||
temp.charAt(i) == 'u' || temp.charAt(i) == 'U')
{
System.out.print(temp.charAt(i).toUpperCase);
}//Obviously wrong but I don't know what to do.
else
{
System.out.print(temp.charAt(i).toLowerCase);
}//Obviously wrong but I don't know what to do.
}
To convert a single character use the methods from the Character class:
System.out.print(Character.toUpperCase(temp.charAt(i)));
System.out.print(Character.toLowerCase(temp.charAt(i)));
Create two final arrays - one with the vowels, the second one with the consonants. Then check, whether the current char in the loop is a vowel or consonant and make the appropriate changes.
Your are hitting your head as String is immutable. Rebuild the resulting string.
A (bit suboptimal) solution is:
public static void parsing(String[] names)
{
for (int i = 0; i < names.length; ++i) {
names[i] = chAngEd(names[i]);
}
}
private static String chAngEd(String s) {
String result = "";
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (ch == 'a' || ...) {
ch = Character.toUpperCase(ch);
} else {
ch = ...
}
result += ch;
}
return result;
}
public static void parsing(String names[]){
for (int i=0; i<names.length; ++i){
names[i] = capitaliseConsts(names[i]);
}
}
private static String capitaliseConsts(String name){
StringBuilder sb = new StringBuilder();
Character c;
for (int i=0; i<name.length(); ++i){
c = name.charAt(i);
if (c.equalsIgnoreCase('a') ||
c.equalsIgnoreCase('e') ||
c.equalsIgnoreCase('i') ||
c.equalsIgnoreCase('o') ||
c.equalsIgnoreCase('u')){
sb.append(Character.toUpperCase(c));
}
else{
sb.append(Character.toLowerCase(c));
}
}
return sb.toString();
}
String vowelsArray = "aeiuo";
String constantsArray = "uppercase constants";
int stringLength = name.length();
String givenNameCopy = name.ToString();
for(int i = 0; i < stringLength; i++){
if(vowelsArray.contains(givenNameCopy[i]))
then uppercase
else if(constantsArray.contains(givenNameCopy[i]))
then lowercase
else
continue;
hope this helps.