So I've naturally checked other questions with this topic, but wasn't able to find the answer to my question that I understood, as I am a complete and utter beginner at coding. I'm trying to write a code for my class (school), but seeing as it's 11:11 at night, my teacher is asleep. Here is my code:
import java.io.*;
import java.text.*;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
public class Unit3_lesson3_1 {
Scanner scanner = new Scanner(System.in);
public void main() {
// code starts here:
System.out.println("This is a program that tells you what your answer is");
System.out.println("Enter the first number ");
int num1 = scanner.nextInt();
System.out.println("Enter the second number ");
int num2 = scanner.nextInt();
System.out.println("Enter your operator. M for mulitiplication, D for division, A for adding, and S for subtraction.");
String op = Scanner.nextLine(); //this is where my error occurs
if (op.equalsIgnoreCase("M")) //this is to check what the string input is
System.out.println("The product is: " + num1*num2);
else if (op.equalsIgnoreCase("D"))
System.out.println("The product is: " + num1/num2);
else if (op.equalsIgnoreCase("A"))
System.out.println("The product is: " + num1+num2);
else if (op.equalsIgnoreCase("S"))
System.out.println("The product is: " + num1-num2);
}
}
The above Scanner.nextLine()
gives me an error stating that a non-static method cannot be referenced in a static context. I really need some help in understanding why, and hopefully you can answer in layman's terms as, like I said, I am a complete beginner.