🔒 Closed Patulong po about char input

Status
Not open for further replies.

GDragon07

Fanatic
Begginer lang po ako sa java, hopefully may makatulong sakin😊
 

Attachments

  • 20190831_211325.webp
    20190831_211325.webp
    115 KB · Views: 27
  • 20190831_211420.webp
    20190831_211420.webp
    134.6 KB · Views: 22
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.

About this Thread

  • 5
    Replies
  • 532
    Views
  • 6
    Participants
Last reply from:
meta29

Online now

Members online
501
Guests online
811
Total visitors
1,312

Forum statistics

Threads
2,275,107
Posts
28,960,677
Members
1,233,595
Latest member
slotherhouse
Back
Top