Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 10th, 2005, 8:49 PM   #1
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
Hey everyone

I recently started learning java.

I don't know if anyone is willing to help me with this, but what I'm basically trying to do is make a combinationLock class, that, if the user enters the combo in the right order, will unlock the lock and display to the user that the lock is now unlocked.

if the user doesn't enter the code right, it just keeps looping through the while loop until the user does.

However, i've tried for the past 2 hours and can't get it to work right.

If anyone's willing to help me, here's my code. I need a way to make the while loop exit when the right letters are entered.

import java.lang.String;

public class CombinationLock
{
    public CombinationLock()
    {
        combo1 = "c";
        combo2 = "f";
        combo3 = "r";
        
        currentTurn = 1;
        
        open = false;        
    }
    
    public void getUserCombo(String turn)
    {
         if (currentTurn == 3)
        {
            if (combo3.equals(turn))
            {
                
                currentTurn = 1;
                open = true;
            }
            
            else
            {
                currentTurn = 1;
                
            }
        }
        
        if (currentTurn == 2)
                {
                    if (combo2.equals(turn))
                    {
                        
                        currentTurn++;
                    }
                    
                    else 
                    {
                        currentTurn = 1;
                        
                    }
                }
                
        if (combo1.equals(turn))
        {
           
            currentTurn++;
        }
        
        else
            currentTurn = 1;
        
        
    }
    
    public boolean checkOpen()
    {
        if (open == true)
            return true;
            
        else
            return false;
    }

private String combo1;
private String combo2;
private String combo3;

private int currentTurn;

private boolean open;
}

//Main.java
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("hey!");
        
        CombinationLock myCombo = new CombinationLock();
        
        int loopRun = 1;
        
        Scanner in = new Scanner(System.in);
        
        while (loopRun == 1)
        {
            boolean isOpen = myCombo.checkOpen();
            
            if (isOpen == true)
                {
                    System.out.println("Congratulation!  You have opened the lock!");
                    loopRun = 2;
                }
                       
            if (isOpen == false)
            {
                System.out.println("Enter a value: ");
                
                String turn = in.next();                             
            
                myCombo.getUserCombo(turn);
            }
        }
    }
}

Thanks everyone

Jason
gardon is offline   Reply With Quote
Old Oct 10th, 2005, 9:51 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
You might want to look into the == operator for strings... i think it'd be a little easier than this solution.
__________________

tempest is offline   Reply With Quote
Old Oct 10th, 2005, 10:06 PM   #3
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
Ok, I redid the whole system, and the code is a lot cleaner.

However, I'm still getting bracket compiler errors, but cant' seem to find out why:

import java.lang.String;

public class CombinationLock
{
    public CombinationLock()
    {
        combo1 = "a";
        combo2 = "c";
        combo3 = "r";
        
        counter = 0;
        
        open = false;
    }
    
    public void getUserInput()
    {
        Scanner in = new Scanner(System.in);
        
        String turn[counter] = in.next();
        
        if (turn[0] == combo1)
            counter++;
            
        if (turn[1] == combo2)
            counter++;
            
        else
            counter = 0;
            
        if (turn[2] == combo3)
            open = true;
            
        else
            counter = 0;
    }
    
    public boolean isOpen()
    {
        if (open == true)
            return true;
            
        else
            return false;
    }
            
            
    
    
    
    
    
    
    private String combo1;
    private String combo2;
    private String combo3;
    
    private String turn[3];
    private int counter;
    
    private boolean open;
}

Main.cpp
public class Main
{
    public static void main(String[] args)
    {
        CombinationLock myCombo = new CombinationLock();
        
        int loopRun = 1;
        
        while(looprun == 1)
        {
            boolean isOpen = myCombo.isOpen();
            
            myCombo.getUserInput();
            
            if (isOpen == true)
                {
                    System.out.println("Congratulations!  You have unlocked it!");
                    loopRun = 2;
                }
                
            else
                System.out.println("Enter a combination value: ");
                
        }
    }
}

and the == strings I've tried other things for. I still get the same erros either way.


Jason
gardon is offline   Reply With Quote
Old Oct 10th, 2005, 10:20 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I don't have a compiler on this machine, but as soon as I get my hands on a machine with a compiler I'll try to help you out.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 10th, 2005, 10:21 PM   #5
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
Alright! I can't use arrays with java just yet... but that's ok! I still have no idea where I went wrong.

Anyways, i found the problem in the original project.

When I checked if (combo.equals(turn)).....

I didn't do the if (currentTurn == 1), so every time it would loop through and assign currentTurn to 1 again


Thanks for the help anyways....

Jason
gardon is offline   Reply With Quote
Old Oct 10th, 2005, 10:21 PM   #6
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
nice to see you again mjordan, lol

as you can tell I've matured a bit
gardon is offline   Reply With Quote
Old Oct 10th, 2005, 10:22 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
One thing I noticed off the bat with your code was:

if (turn[0] == combo1)

You can't compare strings like that. You have to use the equals() method.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 10th, 2005, 10:23 PM   #8
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
I'm also half-way done with a 2D RPG using SDL in c++

But then again... half-way done is only like 10 percent of the final battle (little tid-bits and stuff). I'll send you the final copy when I'm done... which will hopefully be by the end of senior year (because of work and stuff, I don't have as much time as I used to(stuff referring to gay ass school)... ya it's gay ass)

Jason
gardon is offline   Reply With Quote
Old Oct 10th, 2005, 10:24 PM   #9
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Welcome back, Gardon.

You have to initialize your array before you use it. In your constructor, say something like:

turns = new String[length];

Length being however long you need it to be, presumably 3.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 10th, 2005, 10:25 PM   #10
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Quote:
Originally Posted by gardon
I'm also half-way done with a 2D RPG using SDL in c++

But then again... half-way done is only like 10 percent of the final battle (little tid-bits and stuff). I'll send you the final copy when I'm done... which will hopefully be by the end of senior year (because of work and stuff, I don't have as much time as I used to(stuff referring to gay ass school)... ya it's gay ass)

Jason
Good to hear. Looking forward to seeing it. SDL is beautiful, isn't it?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC