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");
		
		
	 }
			  
	}

Recommended Answers

All 7 Replies

Maybe we could if you described the problem, but don't expect any cut-n-paste code answers, of course.

ok.....

start with whats

while (an != A || an != B)

A & B?

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

Okay.
public static what String()
or
public static String what()

Which is it?

can we chat here

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");
  }
}
commented: Incorrect code -1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.