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 4 of 4

Thread: Exception in thread "main" java.langStringIndexOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.langStringIndexOutOfBoundsException

    I'm trying to create a binary to decimal converter. The program also includes a decimal to binary converter, but I'm trying to focus on the binary to decimal converter first. I get an error that says the string index is out of range.

    My program includes one method so far to convert the binary to decimal.



    here's the program if you want to take a look at it.




    import java.util.Scanner;
    import java.lang.Math;
     
     
     
    public class NumberConverter
    {
       public static void main (String[] args)
       {
          Scanner scan = new Scanner(System.in);
          String binarynum;
          int choice;
          int i = 0;
          String binaryreversed = "";
          int decimalnum = 0;
     
          System.out.println("This program will convert binary to decimal or decimal to binary.");
          System.out.println("Please enter a number for what you want to do:"
             + "\n 1. Convert Binary to Decimal."
             + "\n 2. Convert Decimal to Binary."
             + "\n 3. Exit the program.");
             choice = scan.nextInt();
     
        while(choice != 3)
          {
     
             if(choice == 1)//converts binary to decimal
             {
                              decimalnum = ConvertBinarytoDecimal(binaryreversed);  
     
             System.out.println("Enter a Binary number such as \"0111010\". "
             +"\n The program will convert it to decimal.");
             binarynum = scan.nextLine();
     
              while (i < binarynum.length());
              {
               binaryreversed = binaryreversed + binarynum.charAt(i);
               i++;
               }
              }
     
           }
     
        }
     
     
     
     
       public static int ConvertBinarytoDecimal(String binaryreversed)
         { 
            int decimalnum = 0;
            int i = 0;
                while ( i < binaryreversed.length());
                {
                   if(binaryreversed.charAt(i)== '1')
                    {
                    decimalnum = decimalnum + (int)Math.pow(2,i);
                    i++;
                    }
                    else if(binaryreversed.charAt(i)== '0')
                    i++;          
     
                }
                return decimalnum;
     
           }
     
     }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,590
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Exception in thread "main" java.langStringIndexOutOfBoundsException

    an error that says the string index is out of range.
    Please copy the full text of the error message and paste it here.
    If the code is indexing a String the index went out of range. Indexes range in value from 0 to the length-1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    150
    My Mood
    Cheerful
    Thanks
    11
    Thanked 25 Times in 25 Posts

    Default Re: Exception in thread "main" java.langStringIndexOutOfBoundsException

    while ( i < binaryreversed.length());

    That will be executing the ; every time.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Exception in thread "main" java.langStringIndexOutOfBoundsException

    This may be due to the known Scanner "bug". When user types 4<enter> the nextInt method will only read the 4 and leave an empty String in the buffer. Then a call to nextLine will return that empty String. If you attempt to do anything with that String you will get the exception. Solution: flush the buffer after a nextInt call.

    --- Update ---

    Quote Originally Posted by GoodbyeWorld View Post
    while ( i < binaryreversed.length());

    That will be executing the ; every time.
    Well spotted.
    Improving the world one idiot at a time!

Similar Threads

  1. Exception in thread "main" java.lang.NoSuchMethodError: main?
    By Sakharam01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2013, 08:06 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM

Tags for this Thread