• Register
1 vote
39 views

Problem:

I have started learning java programming a few days ago and I doing practice. I wrote a code which throws me some error.
The code I wrote is:
  Scanner scnr = new Scanner(System.in);
     String userInput = " ";
     boolean inputDone = false;

      while (!inputDone) {
         System.out.print("Enter input string: \n");
         userInput = scnr.nextLine();

         if (userInput.equals("a")){
            System.out.println("First word: " + userInput);
            inputDone = true;
         } else  {
                              
            System.out.println("First word: " + userArray[0]);
            System.out.println("Second word: " + userArray[1]);
            
            
            System.out.println();
        }
      }

      return;
 And it shows the error message:
Main.java:1: error: class, interface, or enum expected
  Scanner scnr = new Scanner(System.in);
  ^
Main.java:2: error: class, interface, or enum expected
     String userInput = " ";
     ^
Main.java:3: error: class, interface, or enum expected
     boolean inputDone = false;
     ^
Main.java:5: error: class, interface, or enum expected
      while (!inputDone) {
      ^
Main.java:7: error: class, interface, or enum expected
         userInput = scnr.nextLine();
         ^
Main.java:9: error: class, interface, or enum expected
         if (userInput.equals("a")){
         ^
Main.java:11: error: class, interface, or enum expected
            inputDone = true;
            ^
Main.java:12: error: class, interface, or enum expected
         } else  {
         ^
Main.java:15: error: class, interface, or enum expected
            System.out.println("Second word: " + userArray[1]);
            ^
Main.java:18: error: class, interface, or enum expected
            System.out.println();
            ^
Main.java:19: error: class, interface, or enum expected
        }
        ^
11 errors

It is very confusing to me. Please help me out to solve this thing.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

Everything looks okay in this program. but one thing you have forget that you have to split the string by using comma or by using a space. java splits the strings into two words and then prints it.

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 16 views
16 views
Problem: Kindly someone tell me that how to solve this error .. java error: class, interface, or enum expected
asked Mar 25 Ifra 37.2k points
0 votes
1 answer 26 views
26 views
Thanks for all the help
asked Feb 6 TeamScript 13.5k points
1 vote
1 answer 86 views
86 views
Problem: For the following program of java: public static void main(String[] args) { int A; char a = A; if (Character.isUpperCase()) { System.out.println a + " is an uppercase character."; } } After compiling this code it shows the message: javac -classpath .:/ ... isUpperCase()) { ^ Main.java:6: error: class, interface, or enum expected } ^ 4 errors How to solve this one? Thanks in advance.
asked Apr 27, 2020 samhaz 5k points
1 vote
1 answer 62 views
62 views
Problem: I just started to learn java programming and I am very noob a it. I wrote a code which shows me error Main.java:1: error: class, interface, or enum expected { ^ Main.java:3: error: class, interface, or enum expected public String getName() ^ Main.java:6: ... main { public static void main (String [] args){ system.out.println(emp1.getname()); } } Please help me out to solve this problem.
asked Apr 7, 2020 LizzyM 6.1k points
0 votes
1 answer 6 views
6 views
Who is there to help me explaining what are class, interface, or enum expected?
asked Mar 28 ashik 14.6k points
0 votes
1 answer 3 views
3 views
Problem: Please show me a way to solve this > Class parent int i void display() system.out.println(i)
asked 5 days ago Irenka Kiri 25.6k points
0 votes
1 answer 5 views
5 views
Problem: Hi there! I am a student and I am trying to call a method that is declared in the parent of the parent class, in the child class. I learned how to do this but I cannot recall it. Please help. Here is my attempt: class grand_parent { public void Message() { System. ... .Message(); super.Message(); } } public class Main { public static void main(String args[]) { child ch = new child(); } }
asked May 8 Code Learner 9.9k points
0 votes
1 answer 10 views
10 views
Problem: Now, doesnt it work this way: If I have a try and catch block in the method itself then I need not write method_name(int n) throws Exception?
asked Apr 11 ummesalma 29.2k points
0 votes
2 answers 82 views
82 views
I am new to this. I just want to know how to do that?
asked Nov 6, 2020 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: One of my Macros is giving me the error "class does not support automation or does not support expected interface" when I try to export a Worksheet as PDF. The error lines are: ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\...", Quality:=xlQualityStandard ... somewhere because of the newer version of VB on Windows 10. Does anybody know a way to fix it or a way around it?
asked May 1 muktaa 34.6k points