5 Replies Latest reply: Aug 6, 2009 10:46 PM by 843789 RSS

    Or statement and String

    843789
      Hello All


      Could anyone Explain how i use the or statement with a keyboard inputted string ?

      Here is the line in error
      if (( phrase == Cathy)  || (phrase == Kelvin))
      Also please ignore the comments within the code text.


      import java.util.*;
      public class StringValidity1
      {
         public static void main(String[] args, String Cathy, String Kelvin)
          {
      
              // input the Number
      
             String phrase ;
             
           System.out.print("Enter a Number : ");
              Scanner kybd = new Scanner(System.in);
           phrase = kybd.nextLine();
      
      // process the Number(Integer) see if it is within Range
      
      String Number;
      
       if (( phrase == Cathy)  || (phrase == Kelvin))
      
           Number = "Value is Valid";
      
      else
      
           Number = "Value is Invalid";
      
      System.out.print(Number);
      
      
      }}
      Edited by: Javaman01 on Aug 6, 2009 6:33 PM
        • 1. Re: Or statement and String
          843789
          Problem 1: you shouldn't compare strings with == or !=. Instead use the equals(...) method or equalsIgnoreCase(...)
          • 2. Re: Or statement and String
            843789
            Javaman01 wrote:
            Here is the line in error
            What error?

            If it is not working correctly then advice given above is correct. If you are getting a compile time or run time error then you need to post what that error is.
            • 3. Re: Or statement and String
              800427
              The String object that gets created by the call to kybd.nextLine() will not be the same object as Cathy nor Kelvin.
              That is why comparison by reference as in phrase == Cathy will always fail.
              Those two different String objects however may CONTAIN the same characters!

              To test this you usually use the equals or equalsIgnoreCase method as in:
              if (Cathy.equals(phrase) || Kelvin.equals(phrase)) {
              }
              • 4. Re: Or statement and String
                843789
                Thankyou for your reply :)

                I presumed i had to add the line given but remove the {}

                I tryed this but i got no such method error which i think is caused by "String Cathy, String Kelvin" being in the public static void main part of the coding, problem is i have no idea how to get around this ?

                my attempt at resolving this is the Second Version of the code but i get...

                Exception in thread "main" java.lang.NullPointerException
                at StringValidity1.main(StringValidity1.java:22)

                Which i have never seen before ? and netbeans shows no errors marks in the code , only when run and "Cathy" is entered ??





                import java.util.*;
                public class StringValidity1
                {
                   public static void main(String[] args,String Cathy, String Kelvin)
                    {
                
                        // input the Number
                
                       String phrase ;
                
                     System.out.print("Enter a Number : ");
                        Scanner kybd = new Scanner(System.in);
                     phrase = kybd.nextLine();
                
                // process the Number(Integer) see if it is within Range
                
                String Number;
                
                 if (Cathy.equals(phrase) || Kelvin.equals(phrase))
                
                
                     Number = "Value is Valid";
                
                else
                
                     Number = "Value is Invalid";
                
                System.out.print(Number);
                
                
                }}
                import java.util.*;
                public class StringValidity1
                {
                    private static Object Kelvin;
                    private static Object Cathy;
                    
                public static void main(String[] args)
                    {
                
                        // input the Number
                
                       String phrase ;
                
                     System.out.print("Enter a Number : ");
                        Scanner kybd = new Scanner(System.in);
                     phrase = kybd.nextLine();
                
                // process the Number(Integer) see if it is within Range
                
                String Number;
                
                 if (Cathy.equals(phrase) || Kelvin.equals(phrase))
                
                
                     Number = "Value is Valid";
                
                else
                
                     Number = "Value is Invalid";
                
                System.out.print(Number);
                
                
                }}
                Edited by: Javaman01 on Aug 6, 2009 8:17 PM
                • 5. Re: Or statement and String
                  843789
                  public static void main(String[] args,String Cathy, String Kelvin)
                  You cannot do this as the JVM looks for a method called main that has only one parameter, a String array.

                  If you want to pass values into your program as arguments, then do so via the String array. An example.
                  class ArgsExample {
                      public static void main(String[] args) {
                          if(args.length > 0) {
                              for(int index = 0; index < args.length; index++) {
                                  System.out.println(args[index]);
                              }
                          } else {
                              System.out.println("No arguments");
                          }
                      }
                  }
                  To run:
                  java ArgsExample One Two Three "Four Hundred and Fifty"
                  Edited by: flounder on Aug 7, 2009 1:45 PM
                  Missed a bracket.