this is my final code,, would you pls help me get this right
java Menu driven code
choices only in letters A, B , C

import java.util.*;
public class Letters {


    public static void Menu()
    {
    System.out.println("Menu");
    System.out.println("A. Summary report for a sales employee ");
    System.out.println("B. Print a pattern");
    System.out.println("C. Exit the porgram");
    }	
    
    
    public static  String()
    {
		 String num;
		 char an;
		 Scanner console = new Scanner(System.in);
		 num = console.next();
		 an = num.charAt(0);
		 
		 while (an != A || an != B)
		 {
			System.out.println("Invalid entry Try again") ;
			num = console.next();
		 }
		  
		 return an; 
		  
    }   
	
	public static void main(String[] args)
	{
        String s; 
        char c;
        Scanner console = new Scanner(System.in);
        
         Menu();
         s = console.next();
         c = s.charAt(0);
    
         while (c != C)
                {
                                      
                                                         
                                                           
                 Menu();
                 c = s.charAt(0);

                }
         System.out.println("Goodbye");
		
		
	 }
			  
	}

Edited 6 Years Ago by masijade: added code tags

i'm trying to fix line 14 :
public static String()
every time user enters selection ,
it works with integers fine but doesn't with string and characters,
how do I use Method public static String() as input validation and how do I make a choice using char on the main method..
No No i rather have explanation from experts how to fix this than straight answers it makes me understand the flow better.Thanx for your attention mate

I have updated some of your code, but it is not perfect. There are too many things that need improvement or reorganize your thought. I can see that you are new to the language. I am not sure that you are learning it the right way... You need more reading!

import java.util.*;
public class Letters {

  public static void Menu() {
    System.out.println("Menu");
    System.out.println("A. Summary report for a sales employee ");
    System.out.println("B. Print a pattern");
    System.out.println("C. Exit the porgram");
  }

  // List of mistakes in original version
  // - missing method name!! don't override String class!
  // - you are not comparing character with char but with variable A or B
  // - you get into an infinite loop because you attempt to repeat and accept new
  //   choice when it is not "A" or "B", but fail to use the result in loop
  //   condition again
  // - I added 'C' or you won't be able to get out of the outer while loop
  // - you are expecting to return String, but you are returning char
  public String getAnswer() {
    String num;
    Scanner console = new Scanner(System.in);
    num = console.next();

    // not sure if num contain new line character or not
    // if it does, you won't be able to get out of this loop
    // but I will leave it to you because you need to learn how to deal
    // with this...
    while (num != "A" || num != "B" || num != "C") {
      System.out.println("Invalid entry Try again") ;
      num = console.next();
    }

    return num;
  }

  public static void main(String[] args) {
    String s;
    Do {
      Menu();
      s = getAnswer();
    } while ( s != "C")
    System.out.println("Goodbye");
  }
}

Edited 6 Years Ago by Taywin: n/a

Comments
Incorrect code

Don't forget that if you need to compare Strings use the equals method. The code provided will not work because this: "==" does not work when comparing Strings.

Also another mistake to your code Taywin is that you have getAnswer non static. You cannot call it the way you have it.

Please don't post again erroneous code

This article has been dead for over six months. Start a new discussion instead.