Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,789 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,590 people online right now. Registration is fast and FREE... Join Now!




error using charAt(0)

 

error using charAt(0)

odtwenty4

24 Feb, 2007 - 09:12 AM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2007
Posts: 2


My Contributions

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
+Quote Post


PennyBoki

RE: Error Using CharAt(0)

24 Feb, 2007 - 09:18 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,075



Thanked: 13 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
That means you should catch that exception.
See HERE.

This post has been edited by PennyBoki: 24 Feb, 2007 - 09:18 AM
User is offlineProfile CardPM
+Quote Post

horace

RE: Error Using CharAt(0)

24 Feb, 2007 - 09:59 AM
Post #3

D.I.C Lover
Group Icon

Joined: 25 Oct, 2006
Posts: 1,578



Thanked: 188 times
Dream Kudos: 50
My Contributions
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
+Quote Post

odtwenty4

RE: Error Using CharAt(0)

24 Feb, 2007 - 12:34 PM
Post #4

New D.I.C Head
*

Joined: 24 Feb, 2007
Posts: 2


My Contributions
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
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 08:06PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month