drjava

Why is it saying that my public class is not defined when I only have one public class and the class file


import java.util.Scanner;
public class Lab6_2{
public static void main(String[] args){
  Scanner sc_1 = new Scanner(System.in);
  int numnber, i;
  System.out.println("Enter an integer between 1 and 10.");
  number = sc_1.nextInt();
 }
}

This code is saying "2 errors found: File: C:\Users\danwr\Downloads\lab 6_2.java [line: 2] Error: The public type Lab6_2 must be defined in its own file File: C:\Users\danwr\Downloads\lab 6_2.java [line: 7] Error: number cannot be resolved to a variable"


Solution

  • Based on your error message I see two issues. The first is it appears that your .java file is named "lab 6_2.java" (with a space character). It needs to be named "lab6_2.java". However, be careful with this - Windows doesn't care that you use "lab" vs. "Lab" but on Unix the file would have to be named "Lab6_2.java".

    As for the second error - where is number defined? (and is it spelled correctly)?