School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,878 people online right now. Registration is fast and FREE... Join Now!



error using charAt(0)

error using charAt(0) Rate Topic: -----

#1 odtwenty4  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 24-February 07


Dream Kudos: 0

Posted 24 February 2007 - 09:12 AM

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

  
 
 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.");
 }
 }
   

Was This Post Helpful? 0
  • +
  • -


#2 PennyBoki  Icon User is offline

  • system("revolution");
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 2,075
  • Joined: 11-December 06


Dream Kudos: 500

Expert In: Java,C++,C

Posted 24 February 2007 - 09:18 AM

That means you should catch that exception.
See HERE.

This post has been edited by PennyBoki: 24 February 2007 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • Icon
  • Group: Expert
  • Posts: 1,603
  • Joined: 25-October 06


Dream Kudos: 50

Posted 24 February 2007 - 09:59 AM

after you read the integer with
		   weight = keyboard.nextInt();


it leaves the newline character in the input stream so when you call nextLine()
		 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()
	 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;
	   }
	 }



Was This Post Helpful? 0
  • +
  • -

#4 odtwenty4  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 24-February 07


Dream Kudos: 0

Posted 24 February 2007 - 12:34 PM

Thank you sooo much


View Posthorace, on 24 Feb, 2007 - 10:59 AM, said:

after you read the integer with
		   weight = keyboard.nextInt();


it leaves the newline character in the input stream so when you call nextLine()
		 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()
	 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;
	   }
	 }



Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month