Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,403 Java Programmers. There are 1,502 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

error using charAt(0)

 
Reply to this topicStart new topic

error using charAt(0)

odtwenty4
post 24 Feb, 2007 - 10:12 AM
Post #1


New D.I.C Head

*
Joined: 24 Feb, 2007
Posts: 2




I'm in the inital stage of this program. I'm not getting errors, but when I excute it the program stops at the second iteration giving me this:
StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at HW3.main(HW3.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
>

Thank you

CODE
  

import java.util.Scanner;
public class HW3
{ public static void main (String[] args)
{
     int pass = 1, weight, tot_we = 0;
     String input;
     char choice;
     Scanner keyboard = new Scanner (System.in);
    
     while (pass <= 25 || tot_we <= 1000)
     {
       for (int i=1; i <= 25; i++)
       {
         System.out.println("Passenger " + i);
         System.out.print("\t Do you have any luggage? ");
         input = keyboard.nextLine();
         choice = input.charAt(0);
         if (choice == 'Y' || choice == 'y')
         {
           System.out.print("\n\t How much does it weigh? ");
           weight = keyboard.nextInt();
         }
         pass = pass + i;
       }
     }
     System.out.println("This flight will take off with " + pass + " passengers.");
}
}
  
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


PennyBoki
post 24 Feb, 2007 - 10:18 AM
Post #2


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,006

That means you should catch that exception.
See HERE.

This post has been edited by PennyBoki: 24 Feb, 2007 - 10:18 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

horace
post 24 Feb, 2007 - 10:59 AM
Post #3


D.I.C Addict

Group Icon
Joined: 25 Oct, 2006
Posts: 573

after you read the integer with
CODE

           weight = keyboard.nextInt();

it leaves the newline character in the input stream so when you call nextLine()
CODE

         input = keyboard.nextLine();
         choice = input.charAt(0);

it returns immediatly with an empty string and the charAt() gives an StringIndexOutOfBoundsException
you need to remove the newline, e.g. add a dummy readLine() after the nextTnt()
CODE

     while (pass <= 25 || tot_we <= 1000)
     {
       for (int i=1; i <= 25; i++)
       {
         System.out.println("Passenger " + i);
         System.out.print("\t Do you have any luggage? ");
         input = keyboard.nextLine();
         choice = input.charAt(0);
         if (choice == 'Y' || choice == 'y')
         {
           System.out.print("\n\t How much does it weigh? ");
           weight = keyboard.nextInt();
           input = keyboard.nextLine();  // read newline
        }
         pass = pass + i;
       }
     }



User is offlineProfile CardPM
Go to the top of the page
+Quote Post

odtwenty4
post 24 Feb, 2007 - 01:34 PM
Post #4


New D.I.C Head

*
Joined: 24 Feb, 2007
Posts: 2

Thank you sooo much


QUOTE(horace @ 24 Feb, 2007 - 10:59 AM) *

after you read the integer with
CODE

           weight = keyboard.nextInt();

it leaves the newline character in the input stream so when you call nextLine()
CODE

         input = keyboard.nextLine();
         choice = input.charAt(0);

it returns immediatly with an empty string and the charAt() gives an StringIndexOutOfBoundsException
you need to remove the newline, e.g. add a dummy readLine() after the nextTnt()
CODE

     while (pass <= 25 || tot_we <= 1000)
     {
       for (int i=1; i <= 25; i++)
       {
         System.out.println("Passenger " + i);
         System.out.print("\t Do you have any luggage? ");
         input = keyboard.nextLine();
         choice = input.charAt(0);
         if (choice == 'Y' || choice == 'y')
         {
           System.out.print("\n\t How much does it weigh? ");
           weight = keyboard.nextInt();
           input = keyboard.nextLine();  // read newline
        }
         pass = pass + i;
       }
     }



User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 07:26AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month