-3

This is my code. What I think my error is that method code.

import java.util.Scanner;

public class Question1 {

    public static void main (String[]args){
        String major;
        Scanner read = new Scanner (System.in);
        System.out.print("Enter character : " );
        major=read.next();


    }
}
     public static void mn(){ //method 

        if (major==M){ 
            System.out.println("Mathematic");
        }
        else if (major==C){ 
            System.out.println("Computer Science");
        }
        else if (major==I){ technology
            System.out.println("Information Technology");
        }
        else {
            System.out.println("Invalid Input");
        }            
}
Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
zerofour
  • 33
  • 1
  • 1
  • 4
  • 1
    Your `mn` method appears to be outside your class. Also it appears to be trying to use a bunch of variables that do not exist. – khelwood May 06 '19 at 16:15
  • 1
    Welcome to Stack Overflow! Firstly I would recommend to take the [tour](https://stackoverflow.com/tour) and look around the [help center](https://stackoverflow.com/help), in particular [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). Review your materials and give a try yourself. [Search the solution thoroughly](https://stackoverflow.com/help/searching), and if you're still stuck [post a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). People will be very glad to help. – Carlos Cavero May 06 '19 at 16:18
  • This site is not replacement of basic tutorial for any language. (1) your `mn` method is outside of class (2) you never call that method so it is not included in control flow of your application which starts with main method, (3) inside method it appears you want to compare variables which ware declared inside other methods, or not declared at all (4) maybe by `==M` you meant `=="M"`, in which case you need to read [How do I compare strings in Java?](https://stackoverflow.com/q/513832). – Pshemo May 06 '19 at 16:24

2 Answers2

4

A couple of things are wrong with your code.

  1. Your method mn lies outside of your class. This is invalid.
  2. Strings should be compared with equals
  3. Your method doesn't take any arguments but requires one in this case.
  4. M, C and I need to be String literals, otherwise Java can interpret them as something different.
  5. You actually need to call the method mn to see its output.

So all in all, your code should look like this

import java.util.Scanner;

public class Question1 {

    public static void main (String[]args){
        String major;
        Scanner read = new Scanner(System.in);
        System.out.print("Enter character : " );
        major=read.nextLine(); // better nextLine, because otherwise the press of "Enter" will not be registered.
        mn(major);
    }

    public static void mn(String major){ //method 

        if (major.equals("M")){ 
            System.out.println("Mathematic");
        }
        else if (major.equals("C")){ 
            System.out.println("Computer Science");
        }
        else if (major.equals("I")){
            System.out.println("Information Technology");
        }
        else {
            System.out.println("Invalid Input");
        }            
    }
}
QBrute
  • 4,405
  • 6
  • 34
  • 40
-1

You don't have any calls to mn() in your main function, so the code inside the function won't run. Add the following line to the end of your main function to make the mn() function run, and add "String major" inside the parenthesis of your mn() function declaration:

mn(major);

I hope this helps!

Egrzeda
  • 1
  • 4