CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Posts
    27

    Unhappy Really Stupid Question..

    I know this is a REALLY stupid question, because I only been coding for a year now, and its kind of like... Jr stuff or should be for me, but its like after the summer ended and school opened I forgot.... EVERYTHING! =(

    Anyway...

    How do I make my code reconize the operation for my application?
    Pretty much I have a if and a else statement, and allow the user to type, so if blah blah blah, then say "yup, working fine" else say "nope, you messed up"

    Heres my code with the latest update (probly a stupid mistake im missing but) please tell me if you spot anything wrong with it, thank you...

    Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.Random;
    import java.text.DateFormat;
    import java.util.Date;
    
    public class PINDecoder
    {
        public static void write (String text)
        {
            System.out.print (text);
        }
    
    
        public static void writeLine (String text)
        {
            System.out.println (text);
        }
    
    
        public static void write (int number)
        {
            System.out.print (number);
        }
    
    
        public static void writeLine (int number)
        {
            System.out.println (number);
        }
    
    
        public static void write (char character)
        {
            System.out.print (character);
        }
    
    
        public static void writeLine (char character)
        {
            System.out.println (character);
        }
    
    
        public static String readString ()
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            String var = "";
            try
            {
                var = br.readLine ();
            }
            catch (IOException ioe)
            {
                System.out.println (ioe.toString ());
            }
            return var;
        }
    
    
        public static int readInt ()
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            String tempVar = null;
            try
            {
                tempVar = br.readLine ();
            }
            catch (IOException ioe)
            {
                System.out.println (ioe.toString ());
            }
            int var = Integer.parseInt (tempVar);
            return var;
        }
    
    
        public static char readChar ()
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            char var = 0;
            try
            {
                var = (char) br.read ();
            }
            catch (IOException ioe)
            {
                System.out.println (ioe.toString ());
            }
            return var;
        }
    
    
        public static int randomInt ()
        {
            Random rnd = new Random ();
            int number = rnd.nextInt (1) * 3;
            return number;
        }
    
    
        public static char randomChar ()
        {
            Random rnd = new Random ();
            char character = (char) (rnd.nextInt (1) * 3);
            return character;
        }
    
    
    
    
        /*
    
           write("what you want to write") -writes stuff
           writeLine("what you want to write") -writes stuff and moves to the next line
           readInt() -reads an integer INTO AN INT VARIABLE
           readString() -reads a String INTO A STRING VARIABLE
           randomInt() -generates a random number
           randomChar() -generates a random character
    
    
    
    
        */
        public static void main (String args[])
        {
            String decode;
            String exit;
            int counter;
            int code;
            int one;
            int two;
            int three;
            int four;
    
    
            {
                write ("PIN (Personal Identification Number) Decoder");
                write ("");
                writeLine (" The point of a PIN is to prevent hackers from reaching your info, by passing this simple (or really hard) 4 diget number...");
                write ("This fun little minigame simulates that, by acting as a 'Safe Cracker' You are given unlimited time to attempt to decode the randomly selected 4 diget code.");
                writeLine ("");
                writeLine ("After you get it right, it will tell you how many guesses it took you... Have fun...");
                write ("");
                writeLine ("");
                writeLine ("Type 'Crack' to begin decoding the 4 randomly selected numbers");
                writeLine ("");
                decode = readString ();
                }
                
                if (decode == "Crack" || decode == "crack") {
                write ("This is working correctly");
                }
                
                else {write ("You messed up...");}
    
               
            
        }
    
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Really Stupid Question..

    When comparing objects for equality you should use the equals() method and not ==.
    Note: When comparing strings it can be better to use the equalsIgnoreCase() method provided by the String class which, not surprisingly, tests for equality ignoring the case.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2012
    Posts
    27

    Re: Really Stupid Question..

    Hm...
    equals() or equalsIgnoreCase() didnt seem to do anything...
    Any other ideas on how I might be able to solve my problem?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Really Stupid Question..

    Where did you put the equals()/equalsIgnoreCase() method because it worked when I did it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2012
    Posts
    27

    Re: Really Stupid Question..

    in replacement to == as you said:

    (decode equalsIgnoreCase() "Crack" || decode equalsIgnoreCase() "crack")

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Really Stupid Question..

    That won't even compile so how can you say it didn't seem to do anything?
    Also you don't need both expressions if you are using equalsIgnoreCase - it doesn't matter what case you use ie "cRAcK" would be treated the same as "crack" or "CRACK" etc.

    When calling an object's method you need to use '.' not a space and if you are passing a parameter to the method it needs to be inside the brackets.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured