-2

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)
tmjava
  • 1
  • 1
  • Today is a good day to learn how to debug your code. Start at texting.java line 18 and try to see what is going wrong. – John3136 Jun 29 '16 at 00:34
  • im not sure what is going wrong – tmjava Jun 29 '16 at 00:37
  • Look at the exception "NoSuchElement" - I don't know which is line 18, but somewhere you are asking for something that doesn't exist. Perhaps `nextInt()` when there is no int - I don't know. That's your job. – John3136 Jun 29 '16 at 00:58
  • okay thanks for the tips – tmjava Jun 29 '16 at 00:58
  • This exception will only occur in two situations: you closed `System.in` and then ask for another line, or you manipulated `System.in`, like online IDEs (like ideone.com) do and the predefined input end has been reached. Since both "things" aren't visible in the question, the problem can't be reproduced locally. OP, please create [mcve] and tell us how you run your code. – Tom Jun 29 '16 at 01:48
  • And you should also read this question, because you have another bug in your software: [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/q/7056749) – Tom Jun 29 '16 at 01:51

2 Answers2

0

Just a side note, in Java one typically begins class names with uppercase letters: public class texting should be public class Texting.


Right off the bat, you may want to observe the pattern in your example of optimum functioning. I'll make use of pseudo code to explain the flow:

This should give you a sufficient idea for what is missing in your code.

public class Example
public static void main(String[] args)
{
    // input accountNum
    // input startingBal

    // set updatedBal equal to startingBal

    // input Q/D/W
    // while input is not W
    {
        // if input is W
        {
            // input depositAmt
            updatedBal = updatedBalance(depositAmt);
        }
        // if input is W
        {
            // input withdrawAmt
            updatedBal = updatedBalance(withdrawAmt);
        }

        // print accountNum
        // print updatedBal
    }
}


public static double updatedBalance(int amount)
{ 
    // perform addition or subtraction
}
}
Queue
  • 446
  • 7
  • 23
  • been trying to fix this code but cant seem to get it work – tmjava Jun 29 '16 at 01:19
  • @tmjava I'd suggest placing the while loop in your main method, and then calling updatedBalance(int) only when the user enters D or W. – Queue Jun 29 '16 at 01:21
  • i need the main method to print the account number and the updated account balance after every transaction – tmjava Jun 29 '16 at 01:23
  • @tmjava I updated the pseudo code to be a little more concise – Queue Jun 29 '16 at 01:39
  • okay so then would i put the if statements in the updatedBalance method? – tmjava Jun 29 '16 at 01:50
  • and would i also need to pass my type on in the updatedBalance method – tmjava Jun 29 '16 at 01:52
  • @tmjava or you could try something along the lines of making withdraws negative before you call updatedBalance(int) – Queue Jun 29 '16 at 01:53
  • i need to ask the user the amount deposited or withdrawled – tmjava Jun 29 '16 at 01:55
  • we have left that part out – tmjava Jun 29 '16 at 01:55
  • @tmjava This should occur at `// input depositAmt` and `// input withdrawAmt` – Queue Jun 29 '16 at 01:58
  • @tmjava Please consider marking this answer as correct if it was able to help you. – Queue Jun 29 '16 at 14:06
  • in the updateBalance method how can i know if its addition or subtraction if I am only passing on the depositAmt or withdrawAmt – tmjava Jun 29 '16 at 15:28
  • @tmjava You could consider making the number negative if it is a withdraw, and positive if it is a deposit. – Queue Jun 29 '16 at 15:38
  • okay so my logic here is when the input is equal to W i will set it negative in the while block in the main method next when i pass the value of depositAmt/withdrawAmt to the updatedBalance method wouldnt i need to pass the updatedBal as well? – tmjava Jun 29 '16 at 16:50
  • @tmjava In this case you would need to store your balance as a global variable. – Queue Jun 30 '16 at 16:32
  • @tmjava My point is that there are many ways you can accomplish this task. There is no "right" or "wrong" way of coding. I have been trying to offer hints and offer suggestions without giving you an arbitrary solution that works. This is not a site to ask for straight answers to homework questions. – Queue Jun 30 '16 at 16:36
-1

Wouldn't you need to pass on updatedBal along with depositAmt/withdrawAmt so you can calculate it in th updatedBalance method?

tmjava
  • 1
  • 1
    Welcome to Stack Overflow! This is really a comment, not an answer. With a bit more rep, [you will be able to post comments](//stackoverflow.com/privileges/comment). For the moment I've added the comment for you, and I'm flagging this post for deletion. – Tom Jun 29 '16 at 16:18