Welcome to Dream.In.Code
Become a Java Expert!

Join 149,508 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,334 people online right now. Registration is fast and FREE... Join Now!




Can't find solution for porject please help

2 Pages V  1 2 >  
Reply to this topicStart new topic

Can't find solution for porject please help

ravensrock86
11 Jul, 2007 - 03:59 PM
Post #1

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
Ok. I'm trying to figure out a way to get this program to work, but I can't seem to be able to figure it out.

CODE

import javax.swing.*;

public class ATMAccess {
    public static void main(String[] args)
        String firstCode = "0101", secondCode = "0202";
        int x = 0101;
        int y = 0202;
        
        int accessCode = Integer.parseInt(JOptionPane.showInputDialog("Input access code"));

        if ( (accessCode==x) || (accessCode==y)  ){
            int withdrawal = Integer.parseInt(JOptionPane.showInputDialog("Enter amount to withdraw"));
            JOptionPane.showMessageDialog(null, "Withdrawal authorized");


I tried using if statements, but that has failed to work. I'm considering using boolean statements, but before I do that I want to make sure if there is a way to make this program work using if statement because I am much more familiar with them rather than boolean statements.

This post has been edited by ravensrock86: 11 Jul, 2007 - 03:59 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 06:46 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Can you specify the following:

1. What do you wish the program to do?
2. What is it actually doing that does not conform to point 1?
3. Any error messages you are receiving?
4. The complete code (or is this it? At the very least, you are missing both opening and closing braces in crucial spots that would prevent successful implementation).

Thanks
User is online!Profile CardPM
+Quote Post

ravensrock86
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 07:24 AM
Post #3

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(Amadeus @ 12 Jul, 2007 - 07:46 AM) *

Can you specify the following:

1. What do you wish the program to do?
2. What is it actually doing that does not conform to point 1?
3. Any error messages you are receiving?
4. The complete code (or is this it? At the very least, you are missing both opening and closing braces in crucial spots that would prevent successful implementation).

Thanks


CODE

import javax.swing.*;

public class ATMAccess {
    public static void main(String[] args) {
        String firstCode = "0101", secondCode = "0202";
        int x = 0101;
        int y = 0202;
        
        int accessCode = Integer.parseInt(JOptionPane.showInputDialog("Input access code"));

        if ( (accessCode==x) || (accessCode==y)  ){
            int withdrawal = Integer.parseInt(JOptionPane.showInputDialog("Enter amount to withdraw"));
            JOptionPane.showMessageDialog(null, "Withdrawal authorized");
              
        }
        JOptionPane.showMessageDialog(null, "Invalid access code");
        String option = "Do you want to try another code?";
        int tryAgain = JOptionPane.showConfirmDialog(null, option);
        if (tryAgain == JOptionPane.YES_OPTION){
        int accessCode2 = Integer.parseInt(JOptionPane.showInputDialog("Input access code"));
        
        
            
            
        
        }
        
        }        
            
        
        }


(That is my incompleted code above)
Sorry about that. I was tired when I wrote this and I didn't write out the whole problem.
The part that I needed help on was the if statement. I'm trying to get my program to read that if the acces code entered is either 0101 (the x variable) or 0202 (the y variable) then it will display the withdrawal message below it. But so far, if I enter 0101 or 0202, it will think that the access code is wrong and skip down to the message that tells me that it is an invalid access code. When 0101 or 0202 is used for the access code, I want it to disply "Enter amount to withdraw". Apparently == isn't working. What else can I do?

1. I want the program to act as an ATM machine.
2. Point 1? I don't understand...What is point 1?
3. No error messages.
4. incomplete code.

This post has been edited by ravensrock86: 12 Jul, 2007 - 07:37 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 07:47 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Try printing the values of x,y, and access code. I think you'll see that your leading zeroes have been dropped. You may have to use a String object for comparison.
User is online!Profile CardPM
+Quote Post

ravensrock86
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 08:12 AM
Post #5

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(Amadeus @ 12 Jul, 2007 - 08:47 AM) *

Try printing the values of x,y, and access code. I think you'll see that your leading zeroes have been dropped. You may have to use a String object for comparison.


Thanks. I was able to get the comparison to work. I can't print the values because I have to follow a guideline that makes me Input and Output rather than printing.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 09:00 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
I meant to print them for debugging purposes...the print does not have to be in your final code.
User is online!Profile CardPM
+Quote Post

ravensrock86
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 11:21 AM
Post #7

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(Amadeus @ 12 Jul, 2007 - 10:00 AM) *

I meant to print them for debugging purposes...the print does not have to be in your final code.


ok. i deleted the variables x and y so i dont have to worry about those anymore.

This post has been edited by ravensrock86: 12 Jul, 2007 - 11:22 AM
User is offlineProfile CardPM
+Quote Post

ravensrock86
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 05:11 PM
Post #8

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
This day is not going well for me at all. One problem after another.
CODE

import javax.swing.*;

public class ATMAccess {
    public static void main(String[] args) {
        String firstCode = "0101", secondCode = "0202";
        double maxAmount1 = 500;
        double maxAmount2 = 200;
        String option = "Do you want to try another code?";
        String accessCode = JOptionPane.showInputDialog("Input access code");  
        boolean comparison;
        if ( (comparison = accessCode.equals(firstCode)) || (comparison = accessCode.equals(secondCode))  ){
            int withdrawal = Integer.parseInt(JOptionPane.showInputDialog("Enter amount to withdraw"));
            if ( (comparison = accessCode.equals(firstCode))){
                if (withdrawal <= maxAmount1){
                    JOptionPane.showMessageDialog(null, "Withdrawal authorized");
                }else{
                    JOptionPane.showMessageDialog(null, "Request denied");}}

            if ( (comparison = accessCode.equals(secondCode))){
                if ( (withdrawal <= maxAmount2))
                    JOptionPane.showMessageDialog(null, "Withdrawal authorized");
                else{
                    JOptionPane.showMessageDialog(null, "Request denied");}}
        }
        else{

            while (comparison == false){
                JOptionPane.showMessageDialog(null, "Invalid access code");
                int tryAgain2 = JOptionPane.showConfirmDialog(null, option);
                if (tryAgain2 == JOptionPane.YES_OPTION){    
                    String accessCode2 = JOptionPane.showInputDialog("Input access code");
                    if (comparison =accessCode2.equals(firstCode) || (comparison = accessCode2.equals(secondCode))) {
                        int withdrawal = Integer.parseInt(JOptionPane.showInputDialog("Enter amount to withdraw"));
                        if ( (comparison = accessCode2.equals(firstCode))){
                            if (withdrawal <= maxAmount1)
                                JOptionPane.showMessageDialog(null, "Withdrawal authorized");
                            else{
                                JOptionPane.showMessageDialog(null, "Request denied");}}

                        if ( (comparison = accessCode2.equals(secondCode))){
                            if ( (withdrawal <= maxAmount2))
                                JOptionPane.showMessageDialog(null, "Withdrawal authorized");
                            else{
                                JOptionPane.showMessageDialog(null, "Request denied");}}

                    }



                    else{
                        System.out.println("");





                    }
                }
            }
        }
    }
}


I just can't figure out the problem to this. I've spent about 2 hours and nothing is working.
I'm trying to make the loop stop if I input the right access code, but it's not working. Can someone please help me?




























User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 05:26 PM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
CODE

if ( (comparison = accessCode2.equals(secondCode))){

= is the assignment operator, while == is the equality operator. Are you trying to assign the return value of the method to the variable comparison?
User is online!Profile CardPM
+Quote Post

ravensrock86
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 05:32 PM
Post #10

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(Amadeus @ 12 Jul, 2007 - 06:26 PM) *

CODE

if ( (comparison = accessCode2.equals(secondCode))){

= is the assignment operator, while == is the equality operator. Are you trying to assign the return value of the method to the variable comparison?


To be honest, I don't really understand what you mean. All I know is that it works either way. with == or =.
But can someone please help me figure out my issue with the infinite loop?????????????sad.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 05:36 PM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Your loop has this comparison:
CODE

while (comparison == false){

Where do you set comparison to false?
User is online!Profile CardPM
+Quote Post

ravensrock86
RE: Can't Find Solution For Porject Please Help
12 Jul, 2007 - 05:43 PM
Post #12

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(Amadeus @ 12 Jul, 2007 - 06:36 PM) *

Your loop has this comparison:
CODE

while (comparison == false){

Where do you set comparison to false?


Well, I was just saying that while the comparison was false, then the loop would go on, but when the comparison was true that it would stop. I wasn't sure whether or not that would work so I just tried it.

CODE

if (comparison !=accessCode2.equals(firstCode) || (comparison != accessCode2.equals(secondCode))) {


At first I tried this, but it didn't work.
I actually didn't set it to false. I just tried that because the above failed.
I really have no clue what to do now. sad.gif

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month