![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
You might want to look into the == operator for strings... i think it'd be a little easier than this solution.
__________________
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
nice to see you again mjordan, lol
as you can tell I've matured a bit |
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#10 | |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Quote:
__________________
"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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|