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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Assignment Problem. Please help

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

    Default Assignment Problem. Please help

    Hi

    I need to make a program. Part of it requires the user to enter A B and C and display it. I am trying to make the program accept only A B and C and loop back if another string is entered.

       System.out.println("Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
            Scanner sc = new Scanner(System.in); //Accepts the Users input
            String Selection = sc.nextLine();
     
         if (!Selection.equals("A")){
            do {
     
                    System.out.println("*Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
                    Selection = sc.nextLine();
     
            }
            while (!Selection.equals("A"));
         }   
     
           }
     
        }
    This "works"

    BUT
    When I do   if (!Selection.equals("A") || !Selection.equals("B") || !Selection.equals("C") )
    and

    while  (!Selection.equals("A") || !Selection.equals("B") || !Selection.equals("C") )

    the program doesnt work as intended...


    Any idea's please?


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

    Default Re: Assignment Problem. Please help

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    while (!Selection.equals("A") || ...
    With OR logic if any part of the expression is true, the whole condition is true. If Selection is "B", the posted condition will be true. Look at using AND which requires ALL of the parts of the expression to be true.
    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: Assignment Problem. Please help

    I need either A, B or C to be true though. Not all three? :/

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

    Default Re: Assignment Problem. Please help

    If any one of several conditions should be true, then use an OR.
    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: Assignment Problem. Please help

    ....but then it doesn't work well...I cannot understand why. I spent 2 hours playing around with it.

    if its true... it still outputs the statement

     
    System.out.println("*Please Choose One of the following letters: A, B or C: ");

    which is the code inside the condition....

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

    Default Re: Assignment Problem. Please help

    if its true
    What is the "its" that is true?

    It would be clearer if all the statements you are asking about were posted as they are coded in the program. Posting bits and pieces of code makes for confusion.
    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: Assignment Problem. Please help

    ok, I'll try make myself more clear.

       System.out.println("Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
            Scanner sc = new Scanner(System.in); //Accepts the Users input
            String Selection = sc.nextLine();
     
         if (!Selection.equals("A")){
            do {
     
                    System.out.println("INVALID ----- Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
                    Selection = sc.nextLine();
     
            }
            while (!Selection.equals("A"));
         }   
     
           }
     
        }

    This piece of code works. BUT the problem is that I want the program to run the conditional statement only if A B or C are not entered.
    At the moment, the statement only runs if the statement is NOT A.

    When I edit the statement to become

     
     if (!Selection.equals("A") || !Selection.equals("B") || !Selection.equals("C")){
            do {
     
                    System.out.println("INVALID ----- Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
                    Selection = sc.nextLine();
     
            }
            while (!Selection.equals("A") || !Selection.equals("B") || !Selection.equals("C"));
         }

    The Statement still runs even if I enter A B or C. I cannot understand why the or statement is having this effect on the program.

    Hope this better explains my situation

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

    Default Re: Assignment Problem. Please help

    Look at this one condition: !Selection.equals("A")
    What letters are NOT equal to A ? These are not equal to A: B C D E F G H etc

    In other words, that condition is true for all the rest of the letters in the alphabet besides A

    Add in another condition: !Selection.equals("A") || !Selection.equals("B")
    then one of those two will always be true.
    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: Assignment Problem. Please help

    ahhhhhhhhhhhh....okokok.. can't believe i diidnt see that... I think I got it....

    Thanks.. I'll try it out...

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

    Default Re: Assignment Problem. Please help

    Is it possible to shorten the if statement?

    if (!Selection.equals("A") & !Selection.equals("B") & !Selection.equals("C")){

    I need something like

    !Selection.equals("A" & "B" & "C");
    But it doesnt let me do that. I get a bad operand error

  11. #11
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Assignment Problem. Please help

    See Learning the Java Language > Language Basics > Operators

    & is bitwise AND
    && is logical AND.

    You want logical AND.

    Quote Originally Posted by melki0795
    Is it possible to shorten the if statement?
    You could use regular expressions but adding complexity to shorten code is a poor trade.

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

    Default Re: Assignment Problem. Please help

    What do you mean by regular expressions? May you give me an example? ANy ideas of how I can make this program better so?

  13. #13
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Assignment Problem. Please help

    Regular expressions allow you to test if a string matches a certain pattern. They have their place but try and avoid them if there is an alternative. In your case a couple of if .equals() is more appropriate.

    Quote Originally Posted by melki0795 View Post
    ANy ideas of how I can make this program better so?
    Pen and paper. Write down exactly what you want the program to do, step by step. It's called pseudocode and it's very helpful in wrapping your head around the logic and flow of a program.

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

    Default Re: Assignment Problem. Please help

    Thanks m8...

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assignment Problem. Please help

    Have you read your methodswitch() method? There aren't a lot of options.

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

    Default Re: Assignment Problem. Please help

    import java.util.Scanner;
    public class Interrogator3
    {
        public static void main(String[] args) {
     
     
     
            //Welcoming Message
            System.out.println("Welcome to the program! ");
            System.out.println("");
     
            //The user will be asked to enter his letter of choice.
            System.out.println("Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
            System.out.print("Selection: ");
            Scanner sc = new Scanner(System.in); //Accepts the Users input
            String LetterSelection = sc.nextLine();
            methodswitch(LetterSelection);
         //The Purpose of this condition is to require the user to enter A, B or C and loop until the condition is satisfied.
     
            //This will require the user to enter his name and the string inputted will be stored in the variable 'name'
            System.out.print("Enter your first name: ");
            String name = sc.nextLine();
     
            //This will require the user to enter his age and the integer inputted will be stored in the variable 'age'
            System.out.print("Enter your Age: ");
            int age = sc.nextInt();
     
            //Outputs the information collected in the variables
            System.out.println("");
            System.out.println("Thank You!");
            System.out.println("Your name is: " + name);
            System.out.println("You are " + age + " years old");
            System.out.println("You selected the letter: " + LetterSelection);
     
     
        }
     
        public static void methodswitch(String LetterSelection) {
     
           switch(LetterSelection){
     
           case "A" : 
           case "a" : LetterSelection = LetterSelection; break;
           default : inputMethod(LetterSelection);
     
        }
     
     
      }
     
      public static String inputMethod (String LetterSelection) 
       {
     
     
         System.out.println("____________________________________________________________________________");
         System.out.println("Invalid Selection -- Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
         System.out.print("Selection: ");
         Scanner sc = new Scanner(System.in);
         LetterDelection = sc.nextLine();
     
     
     
         return LetterSelection;
      }
    }

    I tried this. same thing happens. Why dont the methods return the variable? I'm a bit lost

    --- Update ---

    I know there arent options, Im trying to get the program to work...

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Assignment Problem. Please help

    Why dont the methods return the variable?
    Can you be specific? What method and what variable?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Assignment Problem. Please help

    I want the methods to return the variable LetterSelection, back into the the main method so that I could display the letter

  19. #19
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assignment Problem. Please help

    In Java, variable and method names begin with lowercase letters, camelCased thereafter. Class names are capitalized and then camelCased. "methodswitch()" should be called "methodSwitch()," and throughout "LetterSelection" should be named "letterSelection." Not following basic Java naming conventions makes your colde more difficult to read and understand.

    Comments would also be helpful. What is the intended purpose of methdSwitch()? It looks like a half-written input validation method. Half written because it doesn't consider the user's entry of B/b or C/c. Further, the default case can continue to accept and return invalid entries. No validation is enforced within the method.

    In order to use a value returned by a method, it must be assigned to another variable, as in:

    String validSelection = methodSwitch( letterSelection );

    Good luck!

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Assignment Problem. Please help

    want the methods to return the variable LetterSelection
    1)Define the method to return the data type of letterSelection instead of void
    2) have a return statement that returns letterSelection

    The method: inputMethod() does what you are asking about.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Assignment Problem. Please help

    Quote Originally Posted by melki0795 View Post
    I want the methods to return the variable LetterSelection, back into the the main method so that I could display the letter
    But that's not what it is doing:

    public static void methodswitch(String LetterSelection)

    The 'void' part of this method declaration means "I don't return anything". However this isn't the big issue with your code. Take a close look at your inputMethod

     public static String inputMethod (String LetterSelection) 
     {
        //snip
         return LetterSelection;
    }

    It is accepting the String 'LetterSelection' as a parameter and then returning it, without ever actually doing anything (also as Greg noted you should use camelCase for variables and methods to conform with Java conventions).

    Confusingly enough, this doesn't even matter because 'inputMethod' is never called. You are defining a variable elsewhere with the identical name of 'LetterSelection'

    String LetterSelection = sc.nextLine();

    As this thread has progressed I've noticed a tendency to add code when a problem occurs at the expense of the structure and logic flow. Remember what I said about pseudocode? It's so important. Take a step back and think about what your program is supposed to do.

    - Welcome the user
    - Ask the user for a letter 'A', 'B' or 'C'
    - If the letter is not 'A', 'B' or 'C' go back and ask again
    - Ask the user for their name
    - Ask the user for their age
    - Print the letter, the name and age

  22. #22
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assignment Problem. Please help

    Changing the code in previous/earlier posts also makes the conversation more difficult. When I copied the code, methodSwitch() was not a void method. If you want to post updated code, post it on its own rather than revising history.

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

    Default Re: Assignment Problem. Please help

     
     
    import java.util.Scanner;
    public class Interrogator3
    {
      public static void main(String[] args) {
     
     
     
            //Welcoming Message
            System.out.println("Welcome to the program! ");
            System.out.println("");
     
            //The user will be asked to enter his letter of choice.
            System.out.println("Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
            Scanner sc = new Scanner(System.in);
     
            String Letter = sc.nextLine();
            String validLetter = methodswitch(sc, Letter);
            //The Purpose of this condition is to require the user to enter A, B or C and loop until the condition is satisfied.
     
            //This will require the user to enter his name and the string inputted will be stored in the variable 'name'
            System.out.print("Enter your first name: ");
            String name = sc.nextLine();
     
            //This will require the user to enter his age and the integer inputted will be stored in the variable 'age'
            System.out.print("Enter your Age: ");
            int age = sc.nextInt();
     
            //Outputs the information collected in the variables
            System.out.println("");
            System.out.println("Thank You!");
            System.out.println("Your name is: " + name);
            System.out.println("You are " + age + " years old");
            System.out.println("You selected the letter: " + validLetter);
     
     
        }
     
      public static String methodswitch(Scanner sc ,String Letter) {  
     
          String validLetter = "";
     
     
            switch(Letter){
     
             case "A" : 
             case "a" :  validLetter = "a"; break;
     
             default :
                {
                    System.out.println("____________________________________________________________________________");
                    System.out.println("Invalid Selection -- Please Choose One of the following letters: A, B or C: ");//Asks the User for his input
     
                    Letter = sc.nextLine();
                    methodswitch(sc, Letter);
                }
            }
            System.out.println("*"+validLetter);
     
          return validLetter;   
     
        }
     
    }


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


    As you can see, when I output what im trying to return back, it starts off with A... (how it should be) then the variable (validLetter) goes blank....

    hmm... any ideas why this is happening?
    Attached Images Attached Images

  24. #24
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assignment Problem. Please help

    I recommend you rethink your design/logic flow as Christopher suggested. And why are B/b and C/c still not considered valid responses?

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

    Default Re: Assignment Problem. Please help

    I'll try doing that... Will a flowchart be more helpful?

    --- Update ---

    Theyre Not considered as valid responses because I'm trying to get the program to work. I know that the program can work by something similer to the first code i had used in this thread.... But the question requires me to Include A to F and i think by listing them down as 1 condition would be unprofessional.

    I'm talking about this piece of code

     
    if (!Selection.equals("A") & !Selection.equals("B") & !Selection.equals("C")){

    i think that listing a statement something like that all the way to F would be messy....
    that's why im thinking of using a switch statement...

Page 1 of 2 12 LastLast

Similar Threads

  1. New assignment new problem...
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 24th, 2012, 03:00 PM
  2. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 13th, 2012, 08:22 AM
  3. BankAccount assignment problem
    By ddonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 6th, 2011, 01:04 PM
  4. Problem with code for school assignment?
    By Mellisa315 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2010, 09:36 PM
  5. Assignment problem.
    By minou13 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 10:51 PM

Tags for this Thread