CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2014
    Posts
    2

    What would be the correct code for this program?

    The program I am trying to make is a Pig Latin converter. It converts English - Pig Latin.
    When the program restarts then there seems to be an error on line 15. Could you please tell me why and the code to fix it? (I have created this code in Eclipse)


    import java.util.Scanner;

    public class PigLatinConvertor {
    public static void main (String args[]){
    boolean start = true;
    while(start){
    Scanner input = new Scanner(System.in);

    System.out.print("Enter any word: ");

    String word = "a";

    String letter = "y";

    word = input.next();

    int length = word.length();

    char vowel = word.charAt(0);

    if(vowel=='a'||vowel=='A'||vowel=='e'||vowel=='E'||vowel=='i'||vowel=='I'||vowel=='o'||vowel=='O'||vowel=='u'||vowel=='U'){
    System.out.println(word + "way");
    System.exit(0);
    }
    else{
    int i = 0;
    for (i = 0; i < word.length(); i++){
    if (word.charAt(i)=='a' || word.charAt(i)=='e' ||
    word.charAt(i)=='i' || word.charAt(i)=='o' ||
    word.charAt(i)=='u' || word.charAt(i)=='y'){
    break;
    }
    }
    int j = i;
    if (word.contains(letter)){
    if (j == 0){
    String word5 = word.substring(0, j + 1);
    String word6 = word.substring(j + 1, length);
    System.out.println(word6 + word5 + "ay");
    }
    else{
    String word7 = word.substring(0, j);
    String word8 = word.substring(j, length);
    System.out.println(word8 + word7 + "ay");
    }
    }
    else{
    String word3 = word.substring(0, j);
    String word4 = word.substring(j, length);
    System.out.println(word4 + word3 + "ay");
    }
    }
    System.out.println("Do you want to continue? [Y/N]");
    String s = input.next();
    char restart = s.charAt(0);
    if(restart != 'Y') start = false;
    input.close();
    }
    }
    }

  2. #2
    Join Date
    Dec 2013
    Posts
    5

    Re: What would be the correct code for this program?

    Quote Originally Posted by mathgenius1999 View Post
    Code:
    	while(start){
    		Scanner input = new Scanner(System.in);
    Create the Scanner object only one time ... not on every loop.

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
  •  





Click Here to Expand Forum to Full Width

Featured