When I try to compile this program, it highlights \"int = sub;\" and says \"not
ID: 3651137 • Letter: W
Question
When I try to compile this program, it highlights "int = sub;" and says "not a statement". I'm not entirely sure what I'm doing wrong. I'm sure it's a little stupid mistake, but I can't figure it out.Also, I am new to working with the switch class and am unsure if I wrote the code right for that. Just wondering what is wrong about it if it is wrong.
/**
* A program that prompts the user to type in a course number and returns information on a course that number represents
*
* @author Matt
* @version 1.0
*/
import java.util.Scanner;
import java.io.*;
public class Courses {
public static void main(String[] args) {
//Declaring a scanner variable
Scanner info = new Scanner(System.in);
//Assigning labels
String classString;
String instructor;
String credits;
int sub;
System.out.println("This program will tell you more about a course that you may be interested in.");
System.out.println("Please type in the course number that you are interested in: ");
sub = info.next();
int = sub;
switch (class) {
case 160: classString = "Computer Science-I";
instructor = "Pratap Kotala";
credits = "4";
break;
case 114: classString = "Microcomputer Packages";
instructor = "Lisa Bender";
credits = "4";
break;
case 161: classString = "Computer Science II";
instructor = "Sameer Adufardeh";
credits = "4";
break;
case 373: classString = "Computer Organization";
instructor = "Simone Ludwig";
credits = "4";
break;
default: classString = "Invalid Class";
instructor = "Invalid Class";
credits = "Invalid Class";
break;
}
System.out.println("The class you chose is: " +classString);
System.out.println("The instructor of this class is: " +instructor);
System.out.println("The amount of credits for this course is: " +credits);
}
}
Explanation / Answer
"int" is a type declarator. It is a special keyword in Java. What you are doing in that line of code is trying to assign the value of "sub" to "int". Did you mean to say "int class= sub;"? It looks like you're using the "class" variable later on without declaring it, and it looks like on that line you're trying to declare a variable without giving the compiler the name of the variable. In short, I think you meant int class = sub;
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.