In the following code I have been asked to do the following:
1) in main method, to print the account number and the updated account balance after every transaction or until a Q is entered which will quit entering transactinos.
2) in the method updateBalance, given the balance of the account, the type of transaction anmd the amount for the transaction, comput and return the new accountbalance after depositing or withdrawing the given amount
with these instructions I have produced this code:
import java.util.Scanner ;
public class texting
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in) ;
System.out.print("Please enter the account number: ") ;
String accountNumber = in.nextLine() ;
System.out.print("Please enter the initial balance: ") ;
int initialBalance = in.nextInt() ;
double updatedBal = updatedBalance(initialBalance) ;
System.out.println("Account Number: "+ accountNumber+ " Updated Balance: $" + updatedBal) ;
}
public static int updatedBalance(int balance)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter the transaction type/(D to deposit, W to withdraw, Q to quit: ") ;
String type = in.nextLine() ;
while(!"Q".equals(type))
{
System.out.print("Please enter the amount to be deposited or withdrawn: ") ;
int adjustment = in.nextInt();
if((type.equals("D")))
{
balance = balance + adjustment ;
}
else
{
balance = balance - adjustment ;
}
System.out.print("Please enter the transaction type/(D to deposit, W to withdraw, Q to quit: ") ;
type = in.nextLine() ;
}
return balance;
}
}
with the following input:
SAV123
4300
D
1000
D
1100
W
3000
W
2000
Q
it should output:
Please enter the account number: SAV123
Please enter the initial balance: 4300
Please enter the transaction type (D to deposit, W to withdraw, Q to
quit: D
Please enter the amount to be deposited or withdrawn: 1000
Account Number: SAV123 Updated Balance: $5300.0
Please enter the transaction type (D to deposit, W to withdraw, Q to
quit: D
Please enter the amount to be deposited or withdrawn: 1100
Account Number: SAV123 Updated Balance: $5300.0
Please enter the transaction type (D to deposit, W to withdraw, Q to
quit: W
Please enter the amount to be deposited or withdrawn: 3000
Account Number: SAV123 Updated Balance: $2300.0
Please enter the transaction type (D to deposit, W to withdraw, Q to
quit: W
Please enter the amount to be deposited or withdrawn: 2000
Account Number: SAV123 Updated Balance: $2300.0
Please enter the transaction type (D to deposit, W to withdraw, Q to
quit: Q
however my code gives the output:
CompileRunTest: throwable = java.util.NoSuchElementException: No line found
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at texting.updatedBalance(texting.java:18)
at texting.main(texting.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at tester.TesterThread.invokeMain(TesterThread.java:32)
at tester.TesterThread.run(TesterThread.java:38)