0

This is a menu driven program. The user inputs a string and can select either of 2 patterns to be printed using the given string. These are the 2 patterns:

Both patterns

This is the code for my program:

import java.util.Scanner;
    class MenuDrivenPattern
    {
        public static void main()
        {
            System.out.println("\f");
            Scanner sc = new Scanner(System.in);
            while (true)
            {
                System.out.println("\nMENU DRIVEN PROGRAM");
                System.out.println("\nMake your choice");
                System.out.println("\n");
                System.out.println("\nPattern 1");
                System.out.println("\nPattern 2");
                System.out.println("\nExit");
                int choice = sc.nextInt();
                System.out.println("Enter a word");
                String s = sc.next();
                s = s.toUpperCase();
                switch (choice)
                {
                    case 1:
                        for (int a = s.length(); a >= 1; a--)
                        {
                            for (int b = 0; b < a; b++)
                            {
                                char x = s.charAt(b);
                                System.out.print(x);
                            }
                            System.out.println();
                        }
                    case 2:
                        for(int c = 0; c < s.length(); c++)
                        {
                            for (int d = c; d < s.length(); d++)
                            {
                                char y = s.charAt(d);
                                System.out.print(y);
                            }
                            System.out.println();
                        }
                    case 3:
                        System.out.println("Thank you for using this program");
                        System.exit(0);
                    default:
                        System.out.println("Please enter a singular, valid word. Thank you.");
                }
            }
        }
    }

There is the error it is showing:

Error:

Could not find or load main class Main

Caused by: java.lang.ClassNotFoundException: Main

I am relatively new to Java and do not understand this error. What changes do I make to the program to make it compile properly?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Gobardas
  • 1
  • 1
  • The name of the class in the code in your question is `MenuDrivenPattern`. In order to run the code, you need to enter the following command: `java MenuDrivenPattern` from the folder containing the file named `MenuDrivenPattern.class` – Abra Apr 24 '22 at 10:11
  • Your class is called MenuDrivenPattern and your IDE is expecting to launch the program from a Main class. Besides, I see that after your first sc.nextInt() you forgot to place a sc.nextLine(). After reading a number with the Scanner class you must always add an extra nextLine() to get rid of the new line after the number inserted https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Dan Apr 24 '22 at 10:15
  • Sorry @Dan, that didn't change anything. – Gobardas Apr 24 '22 at 10:16
  • @Abra, how do I implement that? If it was convenient, could you please add a snippet of the code? – Gobardas Apr 24 '22 at 10:21
  • Let me rephrase it, your IDE should be expecting to launch the program from a Main class, depends what has been specified. Try what Abra said if you're compiling from command line – Dan Apr 24 '22 at 10:23
  • I'm really sorry, I don't know how to do that. I'm relatively new to Java. What code do I add? – Gobardas Apr 24 '22 at 11:55

0 Answers0