Reputation: 45
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");
}
}
Upvotes: 0
Views: 15617
Reputation: 1
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");
}
}
}
Upvotes: 0
Reputation: 9388
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");
}
}
}
Upvotes: 0
Reputation: 1469
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.
Upvotes: 1