0

I am suppose to write a program that will input a year code from 1 to 4 and output year level.

Note: Using if...else statement and year code is in character type. (if Array is better to use, I am not allowed to use array since this is in the conditional control structures. And after doing that, how can I write this program using switch....case statement?

How can I declare char and get input from the user?

import java.util.Scanner;
import java.io.*;

public class CharStatement {

    public static void main(String[] a) {

    char userInput = new char;
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter year code: ");  
    System.out.println("");

    if (char == 1) {
    System.out.println("First Year");
    System.out.println("Freshmen");     
    }
    else if ( char == 2) {
        System.out.println("Second Year");
        System.out.println("Sophomore");            
    }
    else if (char == 3) {
        System.out.println("Third Year");
        System.out.println("Junior");   
    }
    else if (char == 4) {
        System.out.println("Fourth Year");
        System.out.println("Senior");   
    }
    else {
        System.out.println("Invalid");
    }

}
Finn Donovan
  • 45
  • 1
  • 2
  • 10
  • What have you tried so far? Did you read the Java documentation on [Scanner](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) and did you have a look at the code sample they gave there? – user1438038 Sep 09 '15 at 16:25
  • will try later so I can learn more as I have no knowledge about this at all. Thanks! – Finn Donovan Sep 09 '15 at 16:37

3 Answers3

1

Accepting a single character in java is a little messy. Have a look at this for more on this.

You can do the same thing using an int as shown below as a modification in your code,

System.out.println("");
int choice  = keyboard.nextInt()   // accepts an integer from the user
if (choice == 1) {                 // check if the given input is equal to 1
    System.out.println("First Year");
    System.out.println("Freshmen");     
}

Also, you can't name a variable as char in Java because it is a keyword.

Community
  • 1
  • 1
Ghazanfar
  • 1,353
  • 12
  • 21
  • Thank you @Mohammad Ghazanfar!!! It's now working. Just 2 more questions , does this source code followed the instruction of doing it in character type since we used int? Also, how can I write this program using switch....case statement? kindly start it and I will try to complete it. – Finn Donovan Sep 09 '15 at 16:41
  • I am not sure what you mean when you say `doing it in character type`. Can you be more specific. As for the switch-case, have a look at [this](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) – Ghazanfar Sep 09 '15 at 16:47
  • the direction is this....Using if...else statement and YEAR CODE is in CHARACTER TYPE. Don't know anything as we are still learning the basics but they gave us so many and I'm so confused now. – Finn Donovan Sep 09 '15 at 16:53
0
  1. If you are taking input like numbers then use integer type instead of char type.
  2. You have created the object of the scanner class but you forgot to take the user input from the keyboard.
  3. You can't use char as a variable name as it's a keyword.

So, the modified code using if-else :

    import java.util.Scanner;

    public class IfStmt 
    {
         public static void main(String[] args) 
         {
             Scanner keyboard = new Scanner(System.in);
             System.out.print("Enter year code: ");  
             int year=keyboard.nextInt();
             System.out.println("");

             if (year == 1) 
             {
                 System.out.println("First Year");
                 System.out.println("Freshmen");     
             }

             else if ( year == 2) 
             {
                 System.out.println("Second Year");
                 System.out.println("Sophomore");            
             }

             else if (year == 3) 
             {
                 System.out.println("Third Year");
                 System.out.println("Junior");   
             }

             else if (year == 4) 
             {
                 System.out.println("Fourth Year");
                 System.out.println("Senior");   
             }

             else 
             {
                 System.out.println("Invalid");
             }
        }
    }

Modified code using switch case :

import java.util.Scanner;

public class SwitchStmt 
{
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter year code: ");  
        int year=keyboard.nextInt();
        System.out.println("");

        switch(year)
        {
            case 1:
                System.out.println("First Year");
                System.out.println("Freshmen");
                break;

            case 2:
                System.out.println("Second Year");
                System.out.println("Sophomore");
                break;

            case 3:
                System.out.println("Third Year");
                System.out.println("Junior");
                break;

            case 4:
                System.out.println("Fourth Year");
                System.out.println("Senior");
                break;

            default:
                System.out.println("Invalid");
        }
    }
}
Avijit Karmakar
  • 6,843
  • 5
  • 30
  • 53
0

used this it works. ^_^ see the difference

import java.util.Scanner;

public class GradedExercise34
{
     public static void main(String[] args)
     {
         Scanner keyboard = new Scanner(System.in);
         System.out.print("Enter year code: ");
         char in=keyboard.next( ).charAt(0);
         System.out.println("");

         if (in == '1')
         {
             System.out.println("First Year");
             System.out.println("Freshmen");
         }

         else if ( in == '2')
         {
             System.out.println("Second Year");
             System.out.println("Sophomore");
         }

         else if (in == '3')
         {
             System.out.println("Third Year");
             System.out.println("Junior");
         }

         else if (in == '4')
         {
             System.out.println("Fourth Year");
             System.out.println("Senior");
         }

         else
         {
             System.out.println("Invalid");
         }
    }
}