Buy Needles And Syringes With No Prescription
M4B Store Banner
intex
Riptropin Store banner
Generation X Bodybuilding Forum
Buy Needles And Syringes With No Prescription
Buy Needles And Syringes With No Prescription
Mysupps Store Banner
IP Gear Store Banner
PM-Ace-Labs
Ganabol Store Banner
Spend $100 and get bonus needles free at sterile syringes
Professional Muscle Store open now
sunrise2
PHARMAHGH1
kinglab
ganabol2
Professional Muscle Store open now
over 5000 supplements on sale at professional muscle store
boslabs1
granabolic1
napsgear-210x65
monster210x65
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
DeFiant
UGFREAK-banner-PM
STADAPM
yms-GIF-210x65-SB
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
wuhan2
dpharma
marathon
zzsttmy
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
azteca
crewguru
advertise1x
advertise1x
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store
over 5000 supplements on sale at professional muscle store

java programming supprt, help!!

alan1973

New member
Kilo Klub Member
Joined
Jul 30, 2006
Messages
1,648
I am having a hell of a time with my java project.
It is a simple program where I type in the Transaction type and dollar amount as such:
C 500 (IAW $500 check deposit)
everytime I run it, It starts from the beginning instead of adding on.
If you, the reader, or anyone you know is god at Java let me know. I am really struggling in this class, and the instructor is not being much of a help either.
I will attach the file as a zip.
Thanks in advance.
 

Attachments

  • CheckBook1.zip
    620 bytes · Views: 47
Last edited:
It's been about 8 years 7 years since I coded in java so I'm a little rusty. Your logic is off however. Consider this code segment you have:
<BEGIN CODE>
while ( !trans.equals("Q") )
{
trans = kbd.next( ) ;
cash = kbd.nextInt( ) ;
newBalance1 = cash + 50;
newBalance2 = newBalance1;
if (trans.equals("H") )
{
System.out.println(help);
}
else
System.out.println("Current Balance: $" + newBalance2) ;
}
System.out.println("Thank You for using your CheckBook.\nClosing balance: $");
<END CODE>

newBalance1 gets reset every loop. Since newBalance1 gets reset every loop, you setting newBalance2 inherits it via "=". What you really would want is:
newBalance2=newBalance2 + newBalance1

This way you append to your total.

You could simplify this statement with 1 newBalance integer instead of 2 by wiping out both lines and instantiating:

newBalance=newBalance + cash + 50

Or simplify it more by wiping out cash integer also and instantiating:

newBalance=newBalance + 50 + kbd.nextInt()

(I have no idea what you have 50 in there for. I would imagine it was for testing? If it is remove it)

There are some syntax errors I see in your code, but it probably will still run.
1. No braces to contain your "else" condition statement.
2. No "EndIf" at the end of your condition statement.

Also, wouldn't you want the call the condition for "H" before you check for a second var passed as next integer (nextInt())? This way if they type "H" and nothing after, it wont error out (it may do it silently, but nevertheless it does error out) Check for "H" first, then pull in yout nextInt() within your else condition would probably be the better programming decision.
 
Last edited:
Thanks, I can see this may make a big difference.
I will try these ideas out and let you know how I am doing later.
thanks again!
 
the $50 is the requirement for this assignment.
it is the opening balance.
I tried some of the ideas, but I keep getting initialization errors, but I have no idea how to initialize them before I get into the while statement.
Still working on it though.
 
alan1973 said:
the $50 is the requirement for this assignment.
it is the opening balance.
I tried some of the ideas, but I keep getting initialization errors, but I have no idea how to initialize them before I get into the while statement.
Still working on it though.


Then set newBalance2=50 right after it's declaration and before the while loop. Work on it a little, and if you can't get it, I'll recode it tomorrow morning for you. Your initialization erors are from the errors I mentioned in the previous probably. I figured there were syntax errors when I saw the code. It's like punctuation. Once you get it, you'll be fine.
 
here's my 2 cents.....

Alan, You probably already have completed this assignment but just to add my solution and be helpful, here my answer:

/*
* Main.java
*
* Created on March 31, 2007, 10:08 PM
*
*/

package checkbook;

import java.util.*;

/**
*
* @author Kenneth
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

public static void main(String[] args) {

String trans, help;
int cash;
int newBalance = 50;
boolean myLoop = true; //True
Scanner kbd = new Scanner(System.in);
help = "Help:\nTo enter a deposit, type the letter D followed\n"
+ "by a dollar amount. Example: D 500\n"
+ "To enter a check, type the letter C folowed\n"
+ "by a dollar amount. Example: C 35\n"
+ "To get Help, type H\n"
+ "To quit, type Q";

System.out.println("Welcome to your CheckBook.\nStarting balance: $50\n"
+ "Type H for Help");

System.out.println("------\nCurrent Balance: $50");

while (myLoop) {
trans = kbd.next().toUpperCase();

if (trans.equals("H")) {
System.out.println(help);
} else if (trans.equals("Q")) {
break;
} else if (trans.equals("D")) {
cash = kbd.nextInt();
newBalance = newBalance + cash;
} else if (trans.equals("C")) {
cash = kbd.nextInt();
newBalance = newBalance - cash;
}
System.out.println("Current Balance: $" + newBalance);
}

System.out.println("Thank You for using your CheckBook.\nClosing balance: $" + newBalance);
}
}

I thought Kaiser had good ideals. In fact, my solution came from his points.
Good Luck!
 
solarisdude said:
Alan, You probably already have completed this assignment but just to add my solution and be helpful, here my answer:

/*
* Main.java
*
* Created on March 31, 2007, 10:08 PM
*
*/

package checkbook;

import java.util.*;

/**
*
* @author Kenneth
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

public static void main(String[] args) {

String trans, help;
int cash;
int newBalance = 50;
boolean myLoop = true; //True
Scanner kbd = new Scanner(System.in);
help = "Help:\nTo enter a deposit, type the letter D followed\n"
+ "by a dollar amount. Example: D 500\n"
+ "To enter a check, type the letter C folowed\n"
+ "by a dollar amount. Example: C 35\n"
+ "To get Help, type H\n"
+ "To quit, type Q";

System.out.println("Welcome to your CheckBook.\nStarting balance: $50\n"
+ "Type H for Help");

System.out.println("------\nCurrent Balance: $50");

while (myLoop) {
trans = kbd.next().toUpperCase();

if (trans.equals("H")) {
System.out.println(help);
} else if (trans.equals("Q")) {
break;
} else if (trans.equals("D")) {
cash = kbd.nextInt();
newBalance = newBalance + cash;
} else if (trans.equals("C")) {
cash = kbd.nextInt();
newBalance = newBalance - cash;
}
System.out.println("Current Balance: $" + newBalance);
}

System.out.println("Thank You for using your CheckBook.\nClosing balance: $" + newBalance);
}
}

I thought Kaiser had good ideals. In fact, my solution came from his points.
Good Luck!
will need to plug this into jGrasp to see how it works. I finished the program already and got an A for the class. Your program looks cleaner than mine though.
 
I was wondering how you did on that. You left me hangin...
 
Kaiser said:
I was wondering how you did on that. You left me hangin...
sorry bro, I won't do it again.
Thanks for jumping on my question so fast in the first place!!!
 

Staff online

  • rAJJIN
    Moderator / FOUNDING Member

Forum statistics

Total page views
575,932,742
Threads
138,423
Messages
2,856,342
Members
161,433
Latest member
TheTruth777
NapsGear
HGH Power Store email banner
yourdailyvitamins
Prowrist straps store banner
yourrawmaterials
3
raws
Savage Labs Store email
Syntherol Site Enhancing Oil Synthol
aqpharma
yms-GIF-210x131-Banne-B
hulabs
ezgif-com-resize-2-1
MA Research Chem store banner
MA Supps Store Banner
volartek
Keytech banner
thc
Godbullraw-bottom-banner
Injection Instructions for beginners
YMS-210x131-V02
Back
Top