The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem w/if statements, boolean, & char

3 replies on 1 page. Most recent reply: Feb 28, 2002 4:59 AM by Shelly Olson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Shelly Olson

Posts: 3
Nickname: shelly
Registered: Feb, 2002

Problem w/if statements, boolean, & char Posted: Feb 27, 2002 7:47 PM
Reply to this message Reply
Advertisement
I'm currently taking a Java class, and am still very inexperienced. My assignment is to create an Applet that sends a character to a method, where it is analyzed to decide whether or not it is an alphabetical character. No matter what character I give it, I end up with "true." Can anyone see something obvious I might be overlooking?
Any help would be highly appreciated!
Here is my code:

import java.awt.*;
import java.applet.*;

public class Letters extends Applet
{
public void paint(Graphics g)
{
boolean result;
char letter;
letter = '#';
result = isAlpha(letter);
g.drawString("The character '"+letter+"'",50,50);
if (result = true)
g.drawString("is an alphabetic letter.",50,70);
if (result = false)
g.drawString("is not an alphabetic letter.",50,70);

}

public boolean isAlpha(char input) {
if ((input >= 'a')&&(input <= 'z'))
return true;
if ((input >= 'A')&&(input <= 'Z'))
return true;
return false;

}
}


Shelly Olson

Posts: 3
Nickname: shelly
Registered: Feb, 2002

Re: Problem w/if statements, boolean, & char Posted: Feb 27, 2002 8:11 PM
Reply to this message Reply
I'm posting my code again, formatted so it won't lose its indentations. Sorry about the first time!
import java.awt.*;
import java.applet.*;
 
public class Letters extends Applet
{
 
 
	public void paint(Graphics g)
	{
	   boolean result;
		char letter;
		letter = '#';
	   result = isAlpha(letter);
	   g.drawString("The character '"+letter+"'",50,50);
	   if (result = true)
	   	g.drawString("is an alphabetic letter.",50,70);
		if (result = false)
			g.drawString("is NOT an alphabetic letter.",50,70);
	   	   		   
	}
		
	public boolean isAlpha(char input) {
		if ((input >= 'a')&&(input <= 'z')) 
			return true;
		if ((input >= 'A')&&(input <= 'Z'))
			return true;
		return false;	
	}
}	

Anil S.

Posts: 13
Nickname: anil
Registered: Feb, 2002

Re: Problem w/if statements, boolean, & char Posted: Feb 27, 2002 10:03 PM
Reply to this message Reply
Run the below app. 'n read the comments in the code
below as to where you went wrong.

public class Letters 
{	
   public static void main( String args[] )	
   {	   
     boolean result;		
     char letter;		
     letter = '=';	  
		
     Letters l = new Letters();
		
     result = l.isAlpha(letter);	   
		
     if (result == true)  // This is where you went wrong	   	
     {
	System.out.println( " It is an alphabetic letter." );
     }
     else
     {
	System.out.println(  " It is NOT an alphabetic letter." );
     }			
   }
	
   public boolean isAlpha(char input) 
   {		
     if ( ( input >=  'a' ) && ( input <= 'z' ) || ( input >=  'A' ) && ( input <= 'Z' ) )
     {
        return true;
     }		
 
     return false;		
   }
}	

-Anil

Shelly Olson

Posts: 3
Nickname: shelly
Registered: Feb, 2002

Re: Problem w/if statements, boolean, & char Posted: Feb 28, 2002 4:59 AM
Reply to this message Reply
Thank you so much, Anil!!

Flat View: This topic has 3 replies on 1 page
Topic: how to fax a page.. Previous Topic   Next Topic Topic: running a program from a java application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use