problems in if else

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 910 Views - Last Post: 08 August 2011 - 07:13 PM Rate Topic: -----

#1 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

problems in if else

Posted 03 August 2011 - 03:21 AM

can someone please tell me whats wrong with these code .. i cant see any errors from it ..

System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);
       if((gender1[m]!='f')&&(gender1[m]!='F'))
       {System.out.println("\t PLEASE FOLLOW THE INSTRUCTIONS.");
       System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       else if ((gender1[m]!='m')&&(gender1[m]!='M'))
       {System.out.println("\t PLEASE FOLLOW THE INSTRUCTIONS!");
       System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       else
       {System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       

and the output of it is this:
Enter gender [F/M] for student #1 : F
PLEASE FOLLOW THE INSTRUCTIONS!
Enter gender [F/M] for student #1 : F
Enter grade for student #1 :

why do it is asking twice when I input the right letter .. and it will work after the 2nd input where the letter I input is just the same at first >.<

thanks in advance :) :bigsmile:

This post has been edited by LieannM: 03 August 2011 - 03:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problems in if else

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,135
  • Joined: 05-April 11

Re: problems in if else

Posted 03 August 2011 - 03:44 AM

Are you sure gender1[m]=scan.next().charAt(0); returns the character that you are looking for? try and check it.
Do you really need a char array to store gender(s), when you already have a person object (julie?)

Also I think you should make a do-while loop instead.

do {
	//Code here
} while ( gender1[m].toUpperCase() != 'M'  &&  gender1[m].toUpperCase() != 'F' );


Was This Post Helpful? 1
  • +
  • -

#3 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: problems in if else

Posted 03 August 2011 - 03:48 AM

I already did this also
System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);
       if((gender1[m]!='f'[b])||([/b]gender1[m]!='F'))
       {System.out.println("\t PLEASE FOLLOW THE INSTRUCTIONS.");
       System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       else if ((gender1[m]!='m'[b])||([/b]gender1[m]!='M'))
       {System.out.println("\t PLEASE FOLLOW THE INSTRUCTIONS!");
       System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       else 
       {System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}


its still not working :helpsmilie: :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: problems in if else

Posted 03 August 2011 - 03:56 AM

View PostCasiOo, on 03 August 2011 - 03:44 AM, said:

Are you sure gender1[m]=scan.next().charAt(0); returns the character that you are looking for? try and check it.
Do you really need a char array to store gender(s), when you already have a person object (julie?)

Also I think you should make a do-while loop instead.

do {
	//Code here
} while ( gender1[m].toUpperCase() != 'M'  &&  gender1[m].toUpperCase() != 'F' );




how will I going to check if it is really returning the character that I am looking for?

This post has been edited by LieannM: 03 August 2011 - 03:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,135
  • Joined: 05-April 11

Re: problems in if else

Posted 03 August 2011 - 04:03 AM

View PostLieannM, on 03 August 2011 - 03:54 AM, said:

"Are you sure gender1[m]=scan.next().charAt(0); returns the character that you are looking for? try and check it."

how will I going to check if it is really returning the character that I am looking for?


Just make a System.out.println with it

I was thinking something like this
		Scanner scanner = new Scanner( System.in );
		
		String gender = "";
		do {
			System.out.println( "Enter gender F/M" );
			gender = scanner.nextLine();
		} while ( !gender.toUpperCase().equals( "M" ) && !gender.toUpperCase().equals( "F" ));
		
		julie.setGender( gender.charAt( 0 ) );



or this way (yeah got a little messy)
		Scanner scanner = new Scanner( System.in );
		
		String gender = "";
		while( !(gender = scanner.nextLine()).toUpperCase().equals( "M" ) && !gender.toUpperCase().equals( "F" ) ) {
			System.out.println( "Invalid input! - Try again" );
		}



There are lots of ways, but you will need some sort of loop

This post has been edited by CasiOo: 03 August 2011 - 04:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: problems in if else

Posted 03 August 2011 - 04:09 AM

View PostCasiOo, on 03 August 2011 - 04:03 AM, said:

View PostLieannM, on 03 August 2011 - 03:54 AM, said:

"Are you sure gender1[m]=scan.next().charAt(0); returns the character that you are looking for? try and check it."

how will I going to check if it is really returning the character that I am looking for?


Just make a System.out.println with it

I was thinking something like this
		Scanner scanner = new Scanner( System.in );
		
		String gender = "";
		do {
			System.out.println( "Enter gender F/M" );
			gender = scanner.nextLine();
		} while ( !gender.toUpperCase().equals( "M" ) && !gender.toUpperCase().equals( "F" ));
		
		julie.setGender( gender.charAt( 0 ) );



while ( !gender.toUpperCase().equals( "M" ) && !gender.toUpperCase().equals( "F" ));


this line has an error.. data type char cannot be dereferenced .. and this code
 System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);
       if((gender1[m]!='f')||(gender1[m]!='F'))
       {System.out.println("\t PLEASE FOLLOW THE INSTRUCTIONS.");
       System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       else if ((gender1[m]!='m')||(gender1[m]!='M'))
       {System.out.println("\t PLEASE FOLLOW THE INSTRUCTIONS!");
       System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       else 
       {System.out.print("\tEnter gender [F/M] for student #"+(m+1)+ " :  ");
       gender1[m]=scan.next().charAt(0);
       julie.setGender(gender1[m]);}
       


is inside the for loop .. and the do..while is also not working :/
Was This Post Helpful? 0
  • +
  • -

#7 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: problems in if else

Posted 03 August 2011 - 05:03 AM

(I did not mean to steal your solution, CasiOo)

The while loop is giving an error, because probably your gender parameter is declared as a char instead of a string.

You can either change your gender to a string variable or tweak the loop a little. If you want to use the do...while loop as proposed by CasiOo, then his loop will look like:

while ( !gender.toUpperCase().equals( 'M' ) && !gender.toUpperCase().equals( 'F' ));



as a char is defined as 'char'

This post has been edited by karabasf: 03 August 2011 - 05:20 AM

Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10776
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: problems in if else

Posted 03 August 2011 - 06:31 AM

Quote

while ( !gender.toUpperCase().equals( 'M' ) && !gender.toUpperCase().equals( 'F' ));


If gender is supposed to be a String, then M and F should be surrounded in double-quotes, not single-quotes. Single-quotes denote chars, and double-quotes denote Strings. :)
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: problems in if else

Posted 03 August 2011 - 07:34 PM

You will have to do

gender1[m]=scan.nextLine().charAt(0);

instead of

gender1[m]=scan.next().charAt(0);

if you don't your following scan.next() will read the <LineFeed> at the end of the line
Was This Post Helpful? 0
  • +
  • -

#10 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: problems in if else

Posted 07 August 2011 - 01:11 AM

View Postpbl, on 03 August 2011 - 07:34 PM, said:

You will have to do

gender1[m]=scan.nextLine().charAt(0);

instead of

gender1[m]=scan.next().charAt(0);

if you don't your following scan.next() will read the <LineFeed> at the end of the line




gender1[m]=scan.nextLine().charAt(0);
its not working :dontgetit: and my program has an error if i will use it because i think it only works on string not on char :(
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: problems in if else

Posted 07 August 2011 - 07:49 PM

Depends if gender1 is a char or a String (you changed your code since the begining)

scan.next().charAt(0); and
scan.nextLine().charAt(0); will both return a char

so if gender1 is a String use
gender1[m] = scan.nextLine(); // not scan.next();

and if gender1 is a char use
gender1[m] = scan.nextLine().charAt(0); // not scan.next().charAt(0);

The whole idea, and this is where you error is coming, is that scan.next() does not eat the end of line
Was This Post Helpful? 0
  • +
  • -

#12 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: problems in if else

Posted 08 August 2011 - 07:11 AM

View Postpbl, on 07 August 2011 - 07:49 PM, said:

Depends if gender1 is a char or a String (you changed your code since the begining)

scan.next().charAt(0); and
scan.nextLine().charAt(0); will both return a char

so if gender1 is a String use
gender1[m] = scan.nextLine(); // not scan.next();

and if gender1 is a char use
gender1[m] = scan.nextLine().charAt(0); // not scan.next().charAt(0);

The whole idea, and this is where you error is coming, is that scan.next() does not eat the end of line


my gender1 is declared as char .. hmm i tried to use
gender1[m] = scan.nextLine();
but its not working in char .. it gives error.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10776
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: problems in if else

Posted 08 August 2011 - 07:12 AM

Then you have to assign gender1 = scan.nextLine().charAt(0); to it. You cannot access gender1[m] if gender1 is a char, not an array.

Also, help us help you. Tell us what errors you encounter when you post a problem.
Was This Post Helpful? 0
  • +
  • -

#14 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: problems in if else

Posted 08 August 2011 - 07:20 AM

View Postmacosxnerd101, on 08 August 2011 - 07:12 AM, said:

Then you have to assign gender1 = scan.nextLine().charAt(0); to it. You cannot access gender1[m] if gender1 is a char, not an array.

Also, help us help you. Tell us what errors you encounter when you post a problem.



im sorry my bad i mean gender1 = scan.nextLine().charAt(0); i tried to use this one ..

Enter name for student #1: julie
Enter age for student #1: 17
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Enter gender [F/M] for student #1 : at java.lang.String.charAt(String.java:687)
at Inheritance.Student.studinfo(Inheritance.java:89)
at Inheritance.Inheritance.main(Inheritance.java:31)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)


this is what error im talking about :rolleyes:

This post has been edited by LieannM: 08 August 2011 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10776
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: problems in if else

Posted 08 August 2011 - 07:22 AM

It looks like you hit enter before that prompt. It's a user problem here, not a Java problem.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2