Closed Patulong po about char input

Status
Not open for further replies.

GDragon07

Honorary Poster
Joined
Sep 10, 2016
Posts
166
Reaction
214
Points
130
Age
23
Begginer lang po ako sa java, hopefully may makatulong sakinšŸ˜Š
 

Attachments

Dapat yung sample code mo, ilagay mo sa code block dito at hindi image.

In regards to your code, try to distinguish the difference between a string and a character and how they are quoted.
 
The Problem is dont use (stat=='n')
use (stat.equals("n")) instead


Solution :

Code:
    String letter; //the letter of the user
        Scanner scanner = new Scanner(System.in); //text input scanner
 
        System.out.println("Enter S Single , Enter M Married ");
        System.out.println("Enter Letter: ");
        letter = scanner.next();
        
    
        if (letter.equals("S")) {
                System.out.println("Single");
        }
        else if(letter.equals("M")){
            System.out.println("Married");
        
        }
        else {
        System.out.println("Invalid Letter");
        }
 
Even better put it in a Java REPL. See link here

You do not have permission to view the full content of this post. Log in or register now.

The error tells you what the problem is. If you don't understand the problem, try googling it first. Googling is an important skill if you want to become a developer.
 
import java.util.Scanner;
public class Lovelife{
public static void main(String[]args){

Scanner c =new Scanner(System.in);
String id;

System.out.println("Enter S for Sigle, M for married");

System.out.println("Enter:");
id=c.nextLine();

switch (id){
case "M":
System.out.println("Married");
break;
case "S":
System.out.println("Single");
break;
default:
System.out.print("Invalid input");

}
}
}

//try this
 
Status
Not open for further replies.

Similar threads

Back
Top