Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: AlphaChop Problem

  1. #1
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default AlphaChop Problem

    Hi guys I have this code. It works, but it has a minor glitch which i cannot troubleshoot. Please find the code below followed by an image of my output.

     
    import java.util.Random;
    import java.util.Scanner;
    public class letters
    {
      public static void main(String[] args){
     
     
        Random rn = new Random();
        Scanner sc = new Scanner(System.in);
     
        //generation of a random letter
        char letter = (char)(rn.nextInt(26) + 'A');
        System.out.println("******************"+letter);
     
     
        char alphabet;
        char startingLetter = 'A';
        char endingLetter = 'Z';
        char guessedLetter = '.';
     
        int i = 0;
     
     
     
        //the purpose of this (do, while) loop is for the program to loop while the gussedLetter is incorrect.
       do { 
           //Calling the method generateRange to generate the alphabet
           generateRange(startingLetter, endingLetter);
     
           //Requesting user input
           System.out.println("");
           System.out.println("Please Choose a Letter from above");
           System.out.println("");
           System.out.print("Selection: ");
           guessedLetter = sc.nextLine().charAt(0);
     
           //calling the validation method to validate the character inputted
           guessedLetter = validation(guessedLetter);
     
          //this conditional statement will alter the endingLetter of the alphabet. Once the program loops again it will call the method generateRange again, and the program will display the characters with the modified endingLetter. 
          //The condition will also tell the user that the possible answer is to the left
           if(letter < guessedLetter) {
               System.out.println("");
               System.out.println("Incorrect -- its to the Left of " + guessedLetter+". Your Letters are between: ");
               endingLetter = guessedLetter;
     
           }
          //this conditional statement will alter the startingLetter of the alphabet. Once the program loops again it will call the method generateRange again, and the program will display the characters with the modified startingLetter. 
          //The condition will also tell the user that the possible answer is to the right.
          else if (letter > guessedLetter){
               System.out.println("");
               System.out.println("Incorrect -- its to the Right of " + guessedLetter+". Your Letters are between: ");
               startingLetter = guessedLetter;
     
     
          }
     
          //this counter will keep track of the number of tries it took the user to win!
          i++;
        }
        while (letter != guessedLetter);
     
     
       //Outputs when the answer is correct. and tells the user the number of tries. 
       System.out.println("");
       System.out.println("Correct -- You managed in " + i + " tries");
     
    }
    //the purpose of this method is to generate the alphabet according to the startingLetter and endingLetter which get modified according to the users guesses  
    public static void generateRange(char start, char end){ 
      char alphabet;
      for(alphabet = start; alphabet <= end; alphabet++ ) {
               System.out.print(alphabet+" ");
        }
       System.out.println(""); 
    }
     
      //the purpose of this method is to validate the characters inputted. The program will ask the user to input his guess again if a wrong character is entered, example a number of a symbol.
      //the switch will also convert any lowercase letters to uppercase allowing the user to enter both lower or upercase letters
     public static char guessedLetter = '.';
      public static char validation(char letterValid) {
     
        switch(letterValid) {
            case 'A': 
            case 'a': guessedLetter = 'A'; break;
            case 'B': 
            case 'b': guessedLetter = 'B'; break;
            case 'C': 
            case 'c': guessedLetter = 'C'; break;
            case 'D': 
            case 'd': guessedLetter = 'D'; break;
            case 'E': 
            case 'e': guessedLetter = 'E'; break;
            case 'F': 
            case 'f': guessedLetter = 'F'; break;
            case 'G':
            case 'g': guessedLetter = 'G'; break;
            case 'H': 
            case 'h': guessedLetter = 'H'; break;
            case 'I' :
            case 'i': guessedLetter = 'I'; break;
            case 'J' :
            case 'j': guessedLetter = 'J'; break;
            case 'K': 
            case 'k': guessedLetter = 'K'; break;
            case 'L' :
            case 'l': guessedLetter = 'L'; break;
            case 'M' :
            case 'm': guessedLetter = 'M'; break;
            case 'N' :
            case 'n': guessedLetter = 'N'; break;
            case 'O' :
            case 'o': guessedLetter = 'O'; break;
            case 'P' :
            case 'p': guessedLetter = 'P'; break;
            case 'Q' :
            case 'q': guessedLetter = 'Q'; break;
            case 'R' :
            case 'r': guessedLetter = 'R'; break;
            case 'S' :
            case 's': guessedLetter = 'S'; break;
            case 'T' :
            case 't': guessedLetter = 'T'; break;
            case 'U' :
            case 'u': guessedLetter = 'U'; break;
            case 'V' :
            case 'v': guessedLetter = 'V'; break;
            case 'W' :
            case 'w': guessedLetter = 'W'; break;
            case 'X' :
            case 'x': guessedLetter = 'X'; break;
            case 'Y' :
            case 'y': guessedLetter = 'Y'; break;
            case 'Z' :
            case 'z': guessedLetter = 'Z'; break;
            default : {    //will run if anything apart from A - Z is entered.
                              System.out.println("");
                              System.out.println("Invalid -- Please Choose a Letter!");
                              System.out.println("");
                              System.out.print("Selection: ");
                              Scanner sc = new Scanner(System.in);
                              guessedLetter = sc.nextLine().charAt(0);
                              validation(guessedLetter);
                          }
        } 
        return guessedLetter; 
      }
    }

    This is my output:

    https://dl.dropboxusercontent.com/u/6055003/Capture.PNG

    The problem I have is, for example, when I chose R. The code showed R included in the generated alphabet. After it says that it should be left from R. I need to remove that R.

    Thanks in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    Please copy the output and paste it here. Its hard to copy text from an image.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    Please Choose a Letter from above

    Selection: h

    Incorrect -- its to the Right of H. Your Letters are between:
    H I J K L M N O P Q R S T U V W X Y Z

    Please Choose a Letter from above

    Selection: p

    Incorrect -- its to the Right of P. Your Letters are between:
    P Q R S T U V W X Y Z

    Please Choose a Letter from above

    Selection: t

    Incorrect -- its to the Left of T. Your Letters are between:
    P Q R S T

    Please Choose a Letter from above

    Selection: r

    Correct -- You managed in 4 tries



    Lets take the first selection which was H....
    When it generates the alphabet after, I want it to exclude from outputting H again

    --- Update ---

    any ideas?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    I want it to exclude from outputting H again
    What does that mean? are you asking how to show this String:
    I J K L M N O P Q R S T U V W X Y Z
    How is the first letter to show chosen? It looks like it is off by one.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    it is off by one. It is outputted by a method... called generateRange

    public static void generateRange(char start, char end){ 
      char alphabet;
      for(alphabet = start; alphabet <= end; alphabet++ ) {
               System.out.print(alphabet+" ");
        }
       System.out.println(""); 
    }

    The method is affected by 2 vaiables startLetter and endLetter which change according to the users input (guessedLetter)

     
    if(letter < guessedLetter) {
               System.out.println("");
               System.out.println("Incorrect -- its to the Left of " + guessedLetter+". Your Letters are between: ");
               endingLetter = guessedLetter;
     
           }
          //this conditional statement will alter the startingLetter of the alphabet. Once the program loops again it will call the method generateRange again, and the program will display the characters with the modified startingLetter. 
          //The condition will also tell the user that the possible answer is to the right.
          else if (letter > guessedLetter){
               System.out.println("");
               System.out.println("Incorrect -- its to the Right of " + guessedLetter+". Your Letters are between: ");
               startingLetter = guessedLetter;
     
     
          }


    please refer to the code above

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    Do you see where the value of the parameter sent to the generateRange() method is off by one so it includes the letter you don't want included? For example instead of sending 'H' it should send 'I'


    Did you write this code? Or was it given to you with this problem that you are supposed to fix?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    I wrote the program. So what do you suggest?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    If you wrote it then you know where the value of the starting and ending characters sent to the generateRange() method are set. Look at that code and think about how you want to change it.
    If its too low add 1, if its too high subtract 1
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    YES! Exactly what I tought. Im getting a loss of precision error though. :-( Cannot understand why.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    One Second

    --- Update ---

    if i do

    if(letter < guessedLetter) {
               System.out.println("");
               System.out.println("Incorrect -- its to the Left of " + guessedLetter+". Your Letters are between: ");
               endingLetter = guessedLetter - 1;
     
           }
          //this conditional statement will alter the startingLetter of the alphabet. Once the program loops again it will call the method generateRange again, and the program will display the characters with the modified startingLetter. 
          //The condition will also tell the user that the possible answer is to the right.
          else if (letter > guessedLetter){
               System.out.println("");
               System.out.println("Incorrect -- its to the Right of " + guessedLetter+". Your Letters are between: ");
               startingLetter = guessedLetter + 1;
     
     
          }

    i get an error which says

    Possible loss of precision
    required:char; found: int

    --- Update ---

    '-1' or '+1' will be highlighted

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    Put the expression in () and cast it to char
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    - (char)(1);
    + (char)(1);

    I'm still learning typecasting...still a bit green in it.

    --- Update ---

    are those good? or not even near?

    --- Update ---

    well i still get the same error in that way :-(

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    The expression I was referring to is: guessedLetter + 1
    (char)(guessedLetter + 1)
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    melki0795 (November 19th, 2013)

  16. #15
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: AlphaChop Problem

    Dude. I owe you big time. Thanks for your time!!

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: AlphaChop Problem

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  2. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  3. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  4. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM