Results 1 to 16 of 16
  1. #1
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default Program Freezing While Reading Input

    Hello, I've been away from Java for a little while and am trying to reacquaint myself. For some reason, whenever I try to read input in String form from the console it freezes up my program. My program is also freezing when I am trying to display an input dialog through a JOptionPane. Below is my code. Any help would be greatly appreciated.

    BankAccount class:

    Java Code:
    public class BankAccount 
    {
    	public BankAccount( String myName, int myAccountId, double initialBalance )
    	{
    		accountId = myAccountId;
    		name = myName;
    		balance = initialBalance;		
    	}
    	
    	public void deposit( double amount )
    	{
    		if( amount >= 0 )
    			balance += amount;
    		else System.out.println( "Can't deposit a negative amount.");
    	}
    	
    	public void withdraw( double amount )
    	{
    		if( amount >= 0 )
    		{
    			if ( balance - amount >= 0)
    				balance -= amount;
    			else System.out.println("Can't withdraw more than balance.");			
    		}
    		
    		else System.out.println( "Invalid transaction.");
    	}
    	
    	public double getBalance()
    	{
    		return balance;
    	}	
    		
    	public int getAccountId()
    	{
    		return accountId;
    	}
    
    	public String getName()
    	{
    		return name;
    	}
    	
    	public String toString()
    	{
    		return getClass().getName() + "[" + name + ", " + accountId + ", "
    					+ balance + "]"; 
    	}
    
    	private int accountId;
    	private String name;
    	private double balance;		
    
    }
    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    
    
    public class BankAccountTester
    {
    	public static void main(String[] args)
    	{
    		ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
    		Scanner in = new Scanner( System.in );
    		
    		System.out.print("How many accounts do you want to create? ");
    		int numAccounts = in.nextInt();
    		
    		for( int i = 0; i < numAccounts; i++ )
    		{		
    			System.out.println("What is the name of the account? ");
    			String name = in.nextLine();
    			System.out.println("What is the account ID? ");
    			int iD = in.nextInt();
    			System.out.println("What is the account balance? ");
    			double balance = in.nextDouble();			
    			
    			BankAccount myAccount = new BankAccount( name, iD, balance );
    			accounts.add( myAccount );
    		}
    		
    		System.out.println("done.");
    		
    		JFrame frame = new JFrame();
    		
    		for( int j = 0; j < accounts.size(); j++ )
    		{
    			System.out.println("started JFrame loop.");
    			
    			Object withdrawAmount = JOptionPane.showInputDialog( frame, 
    					"How much would you like to withdraw from account# " + 
    					accounts.get( j ).getAccountId() + "?");			
    			
    			accounts.get( j ).withdraw( (Double) withdrawAmount );
    			
    			Object depositAmount = JOptionPane.showInputDialog( frame, 
    					"How much would you like to deposit from account# " +
    					accounts.get( j ).getAccountId() + "?");
    			
    			accounts.get( j ).deposit( (Double) depositAmount );
    		}
    		
    		for( int k = 0; k < accounts.size(); k++ )
    		{
    			accounts.get( k ).toString();
    		}		
    
    	}
    
    }
    Last edited by airowe; 01-11-2011 at 06:09 AM.

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Are you getting any error messages? Its interesting because I ran it on my machine and it worked just fine, at least the console input part. I'm wondering if its some kind of java installation issue or system issue?
    --user0--

  3. #3
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by user0 View Post
    Are you getting any error messages? Its interesting because I ran it on my machine and it worked just fine, at least the console input part. I'm wondering if its some kind of java installation issue or system issue?
    No, no error messages. So, in.nextLine() is working on your compiler? Weird... :confused:

    What compiler are you using?

  4. #4
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Actually, that's not completely true. Sorry. Whenever I compile, my program automatically outputs the following lines:

    What is the name of the account?
    What is the account ID?

    Then, if I try to enter a string instead of an integer, I get the following error message:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at BankAccountTester.main(BankAccountTester.java:24)

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    yeah it is working on my computer. I am using Eclipse IDE Helios. Maybe it is a delimiter problem. But I don't see why that should cause your system to freeze. Try changing your code to the below see if it works, it just reads line by line instead of nextInt():

    Java Code:
    Scanner in = new Scanner(System.in);
    
    		System.out.print("How many accounts do you want to create? ");
    		int numAccounts = Integer.parseInt(in.nextLine());
    		
    
    		for (int i = 0; i < numAccounts; i++) {
    			
    			System.out.println("What is the name of the account? ");
    			String name = in.nextLine();
    			System.out.println("What is the account ID? ");
    			int iD = Integer.parseInt(in.nextLine());
    			System.out.println("What is the account balance? ");
    			double balance = in.nextDouble();
    
    			BankAccount myAccount = new BankAccount(name, iD, balance);
    			accounts.add(myAccount);
    		}
    --user0--

  6. #6
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    i don't know why you need a JFrame (at least for your example).
    Java Code:
            for( int j = 0; j < accounts.size(); j++ )
            {
                String withdrawAmount = JOptionPane.showInputDialog( null,
                        "How much would you like to withdraw from account# " +
                        accounts.get( j ).getAccountId() + "?");
    
                accounts.get( j ).withdraw( Double.parseDouble(withdrawAmount) );
    
                String depositAmount = JOptionPane.showInputDialog(null,
                        "How much would you like to deposit from account# " +
                        accounts.get( j ).getAccountId() + "?");
    
                accounts.get( j ).deposit( Double.parseDouble( depositAmount )  );
            }

  7. #7
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    That works just fine. Thanks. Not sure why that is any different than my code, but...

    Now, the last thing I can do is enter the account balance. Are there some errors in my JOptionPane code? The code enters the loop, but doesn't progress past this:

    Java Code:
    Object withdrawAmount = JOptionPane.showInputDialog( frame, 
    					"How much would you like to withdraw from account# " + 
    					accounts.get( j ).getAccountId() + "?");

  8. #8
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    Actually, that's not completely true. Sorry. Whenever I compile, my program automatically outputs the following lines:

    What is the name of the account?
    What is the account ID?

    Then, if I try to enter a string instead of an integer, I get the following error message:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at BankAccountTester.main(BankAccountTester.java:24)

    for this, you can use try/catch to catch the exception and , or you can get all inputs as String first, then check if its a number later.

  9. #9
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by charleyjoyce View Post
    i don't know why you need a JFrame (at least for your example).
    Java Code:
            for( int j = 0; j < accounts.size(); j++ )
            {
                String withdrawAmount = JOptionPane.showInputDialog( null,
                        "How much would you like to withdraw from account# " +
                        accounts.get( j ).getAccountId() + "?");
    
                accounts.get( j ).withdraw( Double.parseDouble(withdrawAmount) );
    
                String depositAmount = JOptionPane.showInputDialog(null,
                        "How much would you like to deposit from account# " +
                        accounts.get( j ).getAccountId() + "?");
    
                accounts.get( j ).deposit( Double.parseDouble( depositAmount )  );
            }
    I second what charleyjoyce just said. First of all you don't need a JOptionPane for the deposit and withdrawal. Secondly, you cannot cast String to Double like you were tying to do. The correct way to do it would be to parse:
    Double.parseDouble(withdrawAmount)

    Best,
    --user0--

  10. #10
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by charleyjoyce View Post
    i don't know why you need a JFrame (at least for your example).
    Java Code:
            for( int j = 0; j < accounts.size(); j++ )
            {
                String withdrawAmount = JOptionPane.showInputDialog( null,
                        "How much would you like to withdraw from account# " +
                        accounts.get( j ).getAccountId() + "?");
    
                accounts.get( j ).withdraw( Double.parseDouble(withdrawAmount) );
    
                String depositAmount = JOptionPane.showInputDialog(null,
                        "How much would you like to deposit from account# " +
                        accounts.get( j ).getAccountId() + "?");
    
                accounts.get( j ).deposit( Double.parseDouble( depositAmount )  );
            }
    I tried that. Didn't change anything though. No JOptionPane is displaying. Any idea why?

  11. #11
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    That works just fine. Thanks. Not sure why that is any different than my code, but...

    Now, the last thing I can do is enter the account balance. Are there some errors in my JOptionPane code? The code enters the loop, but doesn't progress past this:

    Java Code:
    Object withdrawAmount = JOptionPane.showInputDialog( frame, 
    					"How much would you like to withdraw from account# " + 
    					accounts.get( j ).getAccountId() + "?");
    Look at charleyjoyce's post for the answer to your question.

    Best,
    --user0--

  12. #12
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    I tried that. Didn't change anything though. No JOptionPane is displaying. Any idea why?
    it works for me. What i only did was,
    1) remove JFrame
    2) Change Object to String
    3) change "frame" to null in JOptionPane.showInputDialog's first argument

    Make sure you import the necessary Swing components as well.

  13. #13
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    I changed everything you said to change, and I'm still getting nothing. Could it be something having to do with my IDE (Eclipse Helios)?

    Here's my updated code:

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    
    
    public class BankAccountTester
    {
    	public static void main(String[] args)
    	{
    		ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
    		Scanner in = new Scanner( System.in );
    		
    		System.out.print("How many accounts do you want to create? ");
    		int numAccounts = Integer.parseInt(in.nextLine());
    		
    		for( int i = 0; i < numAccounts; i++ )
    		{		
    			System.out.println("What is the name of account number " + (i + 1) + "? ");
    			String name = in.nextLine();
    			System.out.println("What is the account ID? ");
    			int iD = Integer.parseInt(in.nextLine());
    			System.out.println("What is the account balance? ");
    			double balance = Double.parseDouble( in.nextLine() );			
    			
    			BankAccount myAccount = new BankAccount( name, iD, balance );
    			accounts.add( myAccount );
    		}
    				
    		for( int j = 0; j < accounts.size(); j++ )
    		{			
    			String withdrawAmount = JOptionPane.showInputDialog( null, 
    					"How much would you like to withdraw from account# " + 
    					accounts.get( j ).getAccountId() + "?");			
    			
    			accounts.get( j ).withdraw( Double.parseDouble( withdrawAmount ) );
    			
    			String depositAmount = JOptionPane.showInputDialog( null, 
    					"How much would you like to deposit from account# " +
    					accounts.get( j ).getAccountId() + "?");
    			
    			accounts.get( j ).deposit( Double.parseDouble( depositAmount ) );
    		}
    		
    		for( int k = 0; k < accounts.size(); k++ )
    		{
    			accounts.get( k ).toString();
    		}		
    
    	}
    
    }

  14. #14
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    sorry i don't use IDE. Run the program through the command prompt and see.

  15. #15
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - i pasted your code into my editor (Also Eclipse Helios) and it worked just fine. Not sure what could be wrong :confused: If you use Scanner for the last part of getting the withdrawal and the deposit amounts, instead of JOptionPane (just a suggestion), see if it will work? Throw some PrintLn statements and see upto which point your code will run, and where it gets stuck.
    --user0--

  16. #16
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by charleyjoyce View Post
    sorry i don't use IDE. Run the program through the command prompt and see.
    Quote Originally Posted by user0 View Post
    Hi - i pasted your code into my editor (Also Eclipse Helios) and it worked just fine. Not sure what could be wrong :confused: If you use Scanner for the last part of getting the withdrawal and the deposit amounts, instead of JOptionPane (just a suggestion), see if it will work? Throw some PrintLn statements and see upto which point your code will run, and where it gets stuck.
    Weird. It works just fine in the console. Must be something wrong with my settings in Eclipse...

    Thanks for the help!

Similar Threads

  1. Problem reading input from cmd.
    By peachyco in forum New To Java
    Replies: 9
    Last Post: 10-14-2010, 06:59 AM
  2. Need help wit reading txt file into input.
    By ShinTec in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:40 PM
  3. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 12:41 PM
  4. reading user input as collection from JSP
    By RSP in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2009, 03:37 PM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •