0

I am working on an assignment converting English to Pig-latin. I am trying to get a word to go if the first letter is a then java should print the whole word. When i type in apple it prints apple but if i put in cool it also prints out cool.

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Assignment_4_Piglatin{

public static void main(String[] args){

Scanner userWord = new Scanner(System.in);

System.out.println("K. Caleb Swallow");
System.out.println("Welcome to the Pig Latin Translator!");

boolean run = true;
while(run){

System.out.println("Please enter a word(or press Q to quit):");

String firstLetter = "something";
String word = userWord.next();

System.out.println(word);

firstLetter = Character.toString(word.charAt(0));

String color = word;


if( firstLetter == "a"){
System.out.println(color);
 }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Jun 03 '17 at 01:59
  • Also, don't use the javascript question tag for your question as it has nothing to do with programming in this language. Question tags and the question title are the two most important choices you would make to initially attract the proper experts to your question, and so you will want to be very precise when using them. I've taken the liberty to remove this tag for you. – Hovercraft Full Of Eels Jun 03 '17 at 02:00
  • Also, in the future, please strive to format your code better. If your code is not easy to read, folks might not bother reading it, so it is to your great advantage to put in the little effort to format it well when posting. Again, I'm posting this in the hopes that the information will help you when you ask future questions here. Please go through the [help] how-to-ask sections for more on best practices on this site. – Hovercraft Full Of Eels Jun 03 '17 at 02:03
  • Thank you! so the correct code would be if(firstLetter equals("a") {...print"word"} – Mr.Swallow3 Jun 03 '17 at 02:04
  • Test it and see what happens... – Hovercraft Full Of Eels Jun 03 '17 at 02:05
  • Note though that `chars` are primitives and so if you stuck with just using them, you could use `==`, e.g. `char firstChar = word.charAt(0); if (firstChar == 'a') {` – Hovercraft Full Of Eels Jun 03 '17 at 02:06
  • shucks.. same result if( firstLetter.equals("a")) { System.out.println(color); } – Mr.Swallow3 Jun 03 '17 at 02:10

0 Answers0