Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: HELP PLEASE

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default HELP PLEASE

    import java.io.*;
    public class gender{
    	public static void main(String [] args) throws Exception{
    	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     
    	char gender;
    	int m=0, f=0;
    	System.out.print("Enter gender: ");
    	gender=(char)br.read();		
     
    	if(gender==f){
    		System.out.println("FEMALE");
    	}
    	if(gender==m){
    		System.out.println("MALE");
     
    	}
     
    	}
    	}


    when i execute this code the output shows press any key to continue

  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: HELP PLEASE

    Hi,

    I've ran this and it does say Enter gender but it won't work beyond that. I'm guessing you want to enter m and it prints MALE and f to print FFEMALE. If you do then you've got gender==f and gender==m
    your problem is here.
    You've also got int m=0, f=0, not sure why these are here.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP PLEASE

    why should i do to run this program?
    when i input F the output is Female and when i input M the output is Male? Please help me

  4. #4
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: HELP PLEASE

    Your problem is your if statements. At the moment you've got gender==f
    And from this statement
    int m=0, f=0;
    f and m are integers.

    f is an int so when you type 'f' it won't match. You need to change it so it will match to the character you type.
    gender=='f' //This is now matching it to the character 'f' not the variable.
    and the same for the other if statement.
    It should work then.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP PLEASE

    Enter gender: f
    FEMALE
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""

    at java.lang.NumberFormatException.forInputString(Num berFormatException.
    java:48)
    at java.lang.Integer.parseInt(Integer.java:489)
    at java.lang.Integer.parseInt(Integer.java:518)
    at gender.main(gender.java:19)
    Press any key to continue...

    why should i do to remover the Exception?

  6. #6
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: HELP PLEASE

    Mine isn't doing that, this is what I've got and its working as you say it should. Can you see what's different from yours`

    import java.io.*;
    public class gender{
    public static void main(String [] args) throws Exception{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     
    char gender;
    System.out.print("Enter gender: ");
    gender=(char)br.read();	
     
    if(gender=='f'){
    System.out.println("FEMALE");
    }
    if(gender=='m'){
    System.out.println("MALE");
     
    }
     
    }
    }

  7. The Following User Says Thank You to samfin For This Useful Post:

    marlontoyo (October 20th, 2011)

  8. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP PLEASE

    ok i done Thanks