Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help scanning a character in JAVA program? I am a beginner and was working on hw

ID: 3623156 • Letter: H

Question

Help scanning a character in JAVA program?
I am a beginner and was working on hw problem.
Assignment: Accept a character. If the character is x return 5. If the character is h return 10. Otherwise return 0. Done!

So far I have the following. Could be wrong all the way but idk.. please help!

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

public class checkChar

{

public static void main (String [] args)

{

//Data Initialize
char a= charAt(0);
Scanner cscan= new Scanner (System.in);

//Welcome Screen
System.out.println("Welcome! Please enter a letter.");
a = cscan.nextChar();
if (a==x)
{
System.out.println(" 5 ");
}
else
{
System.out.println(" 0 ");
}
if (a==h)
{
System.out.println(" 10 ");
}
else
{
System.out.println(" 0 ");
}
return;
}
}

Explanation / Answer

Dear, Here is the modified code import java.util.Scanner; import java.util.*; public class checkChar { public static void main (String [] args) { //Data Initialize char a; Scanner cscan= new Scanner (System.in); String str; //Welcome Screen System.out.println("Welcome! Please enter a letter."); str= cscan.nextLine(); a=str.charAt(0); if (a=='x') { System.out.println(" 5 "); } else if (a=='h') { System.out.println(" 10 "); } else { System.out.println(" 0 "); } } } We don't have nextchar() function of class scanner Need not used if else twice can use else if ladder instead Hope this will help you