Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help in looping

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help in looping



    So, here's my code. I have a runtime error and I think there's a problem in the loop in it but I can't figure it out.



     import java.util.Scanner;
        public class BankSystem
       {
           public static void main(String[] args)
          {
             double acc=1000,d,w;
             int exit, selector;
             char enter;	
             Scanner keyboard = new Scanner(System.in);
             System.out.println("To use the bank system, select 'y' or any other letter to exit.");
             enter = keyboard.nextLine().charAt(0);
             while (enter == 'y')
             {
                System.out.println("Enter one of the following options:");
                System.out.println(" * '1' for account balance.");
                System.out.println(" * '2' for withdraw.");
                System.out.println(" * '3' for deposit.");
                selector = keyboard.nextInt();
                switch (selector)
                {
                   case 1:
                      System.out.println("Your balance is: "+acc);
                      break;
                   case 2:
                      System.out.println("Enter the amount to withdraw.");
                      w = keyboard.nextDouble();
                      if (w<=0 || w>acc)
                         System.out.println("Wrong input.");
                      else
                         acc=acc-w;
                      System.out.println("The selected amount has been withdrawed from your account successfully.");
                      break;
                   case 3:
                      System.out.println("Enter the amount to deposit.");
                      d=keyboard.nextDouble();
                      while (d<=0)
                      {
                         System.out.println("Wrong input.");
                         System.out.println("Enter the right amount.");
                         d = keyboard.nextDouble();
                      }
                      acc=acc+d;
                      System.out.println("The selected amount has been deposited into your account successfully.");
                      break;
                   default:
                      System.out.println("Wrong selection.");
                }
                System.out.println("To continue using the system, select 'y' or any other letter to exit.");
                enter = keyboard.nextLine().charAt(0);
             }
             System.out.println("System is shutting down...");
          }
       }



    Any help or hints on how to correct my error would be great. Thanks.





  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Help in looping

    Whats the runtime error?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in looping

    Quote Originally Posted by Freaky Chris View Post
    Whats the runtime error?

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:686)
    at BankSystem.main(BankSystem.java:49)

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Help in looping

    Clear the input buffer before reading the nextLine, nexInt leaves a \n character in the buffer. Meaning that when you do nextLine no input is read from the user and an empty String is returned thus, when you try to get the letter at 0 you get the exception because the String doesn't have a length of 1, it has a length of 0.

    Chris

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    endframe (December 28th, 2010)

  6. #5
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Help in looping

    Hey endframe,

    The problem here is that, your nextLine() method is trying to read till the end of the line and going to the beginning of second line. And because of that, it is reaching the 1st index. When we enter a single option, we are providing a 0 index value. And this is where the exception is coming from.

    Solution would be to use the simple next() method instead of nextLine() method. So you have to ultimately change two lines of code where you are taking the input options from the console.

    Change your,
    enter = keyboard.nextLine().charAt(0);
    to
    enter = keyboard.next().charAt(0);
    After this the runtime error will be gone. What this does is, it returns the next String token and takes the 0 index value, which is the option we provide.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  7. The Following User Says Thank You to goldest For This Useful Post:

    endframe (December 28th, 2010)

  8. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in looping

    Thanks guys, it worked and now I understand what was wrong.
    Best wishes to all.

Similar Threads

  1. need help looping with a string
    By beandip408 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 29th, 2010, 09:08 AM
  2. Looping with an ArrayList
    By EmSaint in forum Loops & Control Statements
    Replies: 3
    Last Post: September 23rd, 2010, 12:06 AM
  3. Looping through Tokens
    By Viking N7 in forum Loops & Control Statements
    Replies: 2
    Last Post: September 12th, 2010, 12:06 PM
  4. Looping Question
    By miss confused in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 30th, 2010, 12:46 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM