Not sure where the error is coming from
I'm writing a program that uses 2 arrays to store names of flowers, and their corresponding prices. I am new to programming so my coding its really hit or miss. I'm receiving a error when trying to run this program. It compiles fine, runs, through the first "enter flower" and "how many" but then gets an error. Where am I off on this? *note I need to add an else factor to this, I just want to iron out the first part of the if statement first
import java.util.*;
public class FlowerCounter {
public static void main(String[] args) {
String types[]={"petunia","pansy","rose","violet","carnation", "lily", "poppy", "daisy", "tulip", "hydrangea" };
double cost[]={.5,.75,1.5,.75,.85,1.25,2.15,.85,2.75,1.30};
double total=0;
int amt,i;
Scanner s=new Scanner(System.in);
String type;
System.out.print("What type of flower would you like? ");
type = s.nextLine();
for(i=0;i<types.length;i++)
if(type.compareToIgnoreCase(types[i])==0) { //comparing the type to the type in the array types
i=types.length; //setting i's length for use with cost
System.out.print("How many "+type+"s would you like? ");
amt= s.nextInt(); //declaring amount
total = total + amt * cost[i];
System.out.print(type + ":" + amt + "at " + cost[i] + " will cost " + total);
}
}
}
Re: Not sure where the error is coming from
What is the error you are getting? could you please post it.
Re: Not sure where the error is coming from
Quote:
Originally Posted by
Duke_fann
I'm writing a program that uses 2 arrays to store names of flowers, and their corresponding prices. I am new to programming so my coding its really hit or miss. I'm receiving a error when trying to run this program. It compiles fine, runs, through the first "enter flower" and "how many" but then gets an error. Where am I off on this? *note I need to add an else factor to this, I just want to iron out the first part of the if statement first
import java.util.*;
public class FlowerCounter {
public static void main(String[] args) {
String types[]={"petunia","pansy","rose","violet","carnation", "lily", "poppy", "daisy", "tulip", "hydrangea" };
double cost[]={.5,.75,1.5,.75,.85,1.25,2.15,.85,2.75,1.30};
double total=0;
int amt,i;
Scanner s=new Scanner(System.in);
String type;
System.out.print("What type of flower would you like? ");
type = s.nextLine();
for(i=0;i<types.length;i++)
if(type.compareToIgnoreCase(types[i])==0) { //comparing the type to the type in the array types
i=types.length; //setting i's length for use with cost
System.out.print("How many "+type+"s would you like? ");
amt= s.nextInt(); //declaring amount
total = total + amt * cost[i];
System.out.print(type + ":" + amt + "at " + cost[i] + " will cost " + total);
}
}
}
The text in red needs to be removed. Your for loop is moving through i (which starts at 0) incrementally. When you set i to the length of the type array, you're messing up that for loop.
Re: Not sure where the error is coming from
when I run your program , I got a err that " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at test11_23.FlowerCounter.main(FlowerCounter.java:35 )"
FlowerCounter.java:35 is "total = total + amt * cost[i];" so err maybe is cost[i] . so check it
Re: Not sure where the error is coming from
Quote:
Originally Posted by
jcloud
so err maybe is cost[i] . so check it
That is the source of the error. Per my post above, i is out of bounds because it is being set to the length of the type array rather than being set incrementally through the for loop.
Re: Not sure where the error is coming from
@WhiteSoup12
In fact, the program needs get rid of "i=types.length;"
A few days ago, I was told that I should not tell others answer directly , So I gave him ideas ,let him to solve by himself
by the way What does "iron out" mean ?
Re: Not sure where the error is coming from
Why don't you try changing the place of this line of code a little down?
Try to understand the exception: ArrayIndexOutOfBounds