Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Chase R

macrumors 65816
Original poster
May 8, 2008
1,279
81
PDX
I just finished writing this program for my java class. It's a simple program that calculates how much it costs to install a fence. At the end, it asks "would you like to make another estimate (Y/N)", and the user is suppose to choose yes or no. However, after I'm done running mine, I won't get that option, it just says:

Calculate Another Estimate (Y/N): Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:558)
at Test1.main(Test1.java:73)

Why is it doing this!!!

Here is the program by the way:

Code:
public class Test1
{
	public static void main( String[] args )
	{
		String customer = " ";
		String customerUC = " ";

		String type1 = " ";
		String type2 = " ";
		char type = ' ';

		String another1 = " ";
		char another = 'y';
		
		int fence = 0;
		int count = 0;

		double costFoot = 0;
		double costTotal = 0;
		double costGrand = 0;

		Scanner read = new Scanner(System.in);

		System.out.print( "\n\t----------SIERRA FENCING COMPANY----------" );

		while ( Character.toUpperCase(another) == 'Y' )
		{
			System.out.print( "\n\nYour Name: " );
			customer = read.nextLine();
			customerUC = customer.toUpperCase();

			System.out.print( "\nNew Installation (N) or Repair (R): " );
			type1 = read.nextLine();
			type2 = type1.toUpperCase();
			type = type2.charAt(0);

			System.out.print( "\nAmount of Fencing Needed (linear feet): " );
			fence = read.nextInt();

			System.out.print( "\nCost-Per-Foot of Fencing: " );
			costFoot = read.nextDouble();

			if ( costFoot > 6 && type == 'N' )
			{
				costFoot = 6;
			}

			else if ( costFoot > 5 && type == 'R' )
			{
				costFoot = 5;
			}
			
			costTotal = fence * costFoot;
			costGrand += costTotal;
			count++;

			System.out.print( "\n\n-------------------------------------------------------" );
			System.out.print( "\n\tName: " + customerUC );
			System.out.print( "\n\tType: " + type );
			System.out.print( "\n\tLinear Fencing: " + fence + " feet" );
			System.out.print( "\n\tCost-Per-Foot: $" + costFoot );
			System.out.print( "\n\tTotal: $" + costTotal );
			System.out.print( "\n-------------------------------------------------------" );

			System.out.print( "\n\nCalculate Another Estimate (Y/N): " );
			another1 = read.nextLine();
			another = another1.charAt(0);
			
		}
		
		System.out.print( "\n\nGrand Total: $" + costGrand );
		System.out.print( "\nYou made " + count + " estimates" );
	}
}
 

Littleodie914

macrumors 68000
Jun 9, 2004
1,813
8
Rochester, NY
Wow, that's tricky. I got it to work by changing your last "read.nextLine()" to just "read.next()", but I'll look a bit closer to see why it doesn't work the first way.

Edit: It seems to have something to do with the nextLine() method advancing the pointer beyond the current line. I don't understand it that much myself, but these links might help clear up a bit of confusion:

Scanner API
Related Forum Post
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
I'm going to guess that one of your your previous read calls are leaving the newline character in the keyboard input buffer. Therefore, when you call nextLine(), you get an empty string, leading to the exception trying to examine character at position 0, since there is none. This is also a problem with many of the console input C functions. Somehow you probably want to flush the buffer first with a dummy read call or something. I haven't used Java in a long time so I don't know what the right procedure would be exactly.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
I just finished writing this program for my java class. It's a simple program that calculates how much it costs to install a fence. At the end, it asks "would you like to make another estimate (Y/N)", and the user is suppose to choose yes or no. However, after I'm done running mine, I won't get that option, it just says:

Calculate Another Estimate (Y/N): Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:558)
at Test1.main(Test1.java:73)

Why is it doing this!!!

Since you have the source code, probably in a program editor that can be made to display line numbers, you should be able to find where line 73 in the file Test1.main is. That will show you where the problem happens.

Now you have an exception that says an index of 0 is "out of range". If you access a string with n characters, what are the valid indices?

Now can you tell us in exactly which situation is the index 0 not a valid index?

That should answer your question.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
Since you have the source code, probably in a program editor that can be made to display line numbers, you should be able to find where line 73 in the file Test1.main is. That will show you where the problem happens.

The line is:

Code:
another1 = read.nextLine();

right after it displays "Calculate Another Estimate (Y/N):"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.