PDA

View Full Version : "String index out of range: 0" ...Can someone tell me what's wrong here? (Java)




Chase R
Jun 2, 2009, 05:58 PM
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:

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
Jun 2, 2009, 06:49 PM
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 (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#nextLine())
Related Forum Post (http://www.coderanch.com/t/404189/Java-General-beginner/java/Scanner-nextline-Help)

HiRez
Jun 2, 2009, 07:07 PM
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
Jun 2, 2009, 07:31 PM
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
Jun 2, 2009, 07:34 PM
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:

another1 = read.nextLine();

right after it displays "Calculate Another Estimate (Y/N):"