Page 1 of 1

boolean functions part 2(working with string) Rate Topic: -----

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Posted 20 March 2009 - 01:23 PM

Well, welcome back to the booleans tutorial. Thank you for stopping by again. In this second tutorial, we will be taking a look at how we can use strings to see if something is true or false.

Before we do, let's review a little bit on the structure of a boolean.

A boolean always starts with a constructor, which just allows JAVA to do that process.

The constructor for a boolean looks like:
public static boolean [name of boolean] ( [type of data] [data name] );



Since we are going to be working with strings, the constructor would look like:
public static boolean nameCheck (String answer);




We have to use public static because we are writing a method and the type of method is boolean

*just a side note, and i'm sure another tutorial says something about this*
a function or method in java must start with public static because it is apart of the constructor, which I mentioned earlier.

We also use void because when the main method sees that sub-method, it stops then goes to where it we defined it and does what we told that sub-method to do.

Anyway, so what is this function going to do specifically? We will be checking if an entered string is the same as a constant string.

The boolean would look like:
public static boolean nameCheck(String answer)
{
   boolean yes=true;
   String finalName=new String("fred");
   int finalAnswer;
   
   finalAnswer=answer.compareTo(finalName);
   
   if(finalAnswer!=0)
   {
      yes=false;
   }
   
   
   return yes;   
}
  


Notice that in JAVA we can't just if(name=="fred"). At least I have never seen it, so we use compareTo() which will make it into an integer and if we put it into an if statement we could have a couple of possibilities:

1! The strings will equal 0 which means that they are equal.
2! One of them is bigger than another which would mean that they are not equal to each other.

*NOTE: If you notice, we are still going to use an int to check if the comparison is true or not, we just use String because that is the type of variable we are putting into the sub-method.*

Okay, so what do we want to do after we checked if the comparison is true or not?

We want to let the user know because in the boolean it just checks if it is true or not, it doesn't display anything. So the main method would look like:
public static void main(String args[])
{
   String answer;
   Scanner in=new Scanner(System.in);

   System.out.print("Please enter name: ");
   answer=in.nextLine();

   if(nameCheck(answer))
   System.out.println("Hello Fred!");

   else
   System.out.println("You are not Fred, therefore do not have access to this section.");
}



*NOTE: In java there is no cin>>. Instead we use a Scanner.*

How would this look when we put it all together?

imort java.util.*;

public class stringBoolean
{
  public static boolean nameCheck(String answer)
{
   boolean yes=true;
   String finalName=new String("fred");
   int finalAnswer;
   
   finalAnswer=answer.compareTo(finalName);
   
   if(finalAnswer!=0)
   {
      yes=false;
   }
   
   
   return yes;   
}
 public static void main(String args[])
{
   String answer;
   Scanner in=new Scanner(System.in);

   System.out.print("Please enter name: ");
   answer=in.nextLine();

   if(nameCheck(answer))
   System.out.println("Hello Fred!");

   else
   System.out.println("You are not Fred, therefore do not have access to this section.");
 }
}



The only difference between the string boolean and the int boolean is the type of data you are putting into the boolean.

The first two booleans we did were just checking if it was true (the boolean itself and in the main we were doing stuff with the boolean value). There are a couple more things we can do with these types of functions, but in order to learn those, we had to start from the basics-thus the first two tutorials.

Thank you again for stopping by, please come back for yet another part of these interesting functions.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1