0

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.

Danpe
  • 18,668
  • 21
  • 96
  • 131
  • 4
    try scanner.nextLine() (first letter in lower case) – kkhipis May 06 '16 at 03:18
  • 2
    `Scanner` is a class and `scanner` is the instance/object on which you want to be calling `nextLine()` on. – Harsh Poddar May 06 '16 at 03:21
  • Correct capitalization is mandatory in most programming languages. Variable `bob` and `bOb` are two completely different things with different ascii characters making them up. Just a helpful hint for your future programming ventures. Good luck with your assignment, please try to keep capitlization correct. Also, I'd recommend you learn about variable [naming conventions](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) in java. – Ashwin Gupta May 06 '16 at 03:36

0 Answers0