0

I'm doing a project of If and else,This project requires you to take a user input, and then move the first letter of the word they entered to the last letter of the word. The problem is I don't know what the last number of the user is.

I want to use the substring method for this. I don't know how to move a letter from the beggining of the word to the last letter of the word , because I don't know what is the last letter of the word(the user can enter anything)

I started doing it but I have no progress at all, because I don't know what the last letter is of the users input.

import javax.swing.*;
public class PigLatinDriver {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String word = JOptionPane.showInputDialog("Enter the word to be translated into pig Latin.");


    boolean consonant =false;

    If(word.substring(0)== "b");{
        word.substring(0).replace("b"," ");

    }
    if(consonant==true){

    }

    //perform the logic to translate to piglatin
    //Words that begin with a consonant move the first letter to the end and then add ay

    // vowels just add ay
    // The exception being when you use the word "Small" with an upper case, then you make no change.

    String translation = "";

    JOptionPane.showMessageDialog(null, translation);
}

private static void If(boolean b) {
    // TODO Auto-generated method stub

}

}
Lain
  • 2,166
  • 4
  • 23
  • 47
  • 1
    Well, for starters, `word.substring(0)== "b"` is the wrong way to go for comparing strings. See [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Dennis Meng Oct 18 '13 at 01:15
  • Thankyou:D that helps but any idea how to move the front letter to the back without knowing the exact ending of a word? I feel like i should use greater then and less then but I think it wont come up right –  Oct 18 '13 at 01:17
  • 1
    Do you strictly need to know what the end of the word is to add a letter to the end? – Dennis Meng Oct 18 '13 at 01:18
  • No you dont, but how will i type it into the substring, is there a name for the ending of a word? I basically need to know the ending of a word or check where the word ended. –  Oct 18 '13 at 01:19
  • Still not really necessary. All you need is to be able to split the string into the first character and the rest of the string. You don't need to know the ending of the word for that. – Dennis Meng Oct 18 '13 at 01:24
  • Okay I already know how to get rid of it from the begging of the word(the first letter) but now How do i add it to the end? –  Oct 18 '13 at 01:25
  • `+` can also be used for string concatenation in Java. (though there's also a `concat` method if you really want to use it) – Dennis Meng Oct 18 '13 at 01:29
  • May you please use an basic example please –  Oct 18 '13 at 01:31

1 Answers1

0

you can simply concat the string using concat() method from String API. OR you can just simply use the the '+' operator for string.

Try this out:

Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); //get input from user

char firstLetter = input.charAt(0); //get the first letter
input = input.substring(1); //remove the first letter from the input string
input = input + firstLetter + "ay"; //add first letter and "ay" to end of input string

:)

CodingBird
  • 705
  • 2
  • 11
  • 22