Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Not sure where the error is coming from

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

    }


    }
    }


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Not sure where the error is coming from

    What is the error you are getting? could you please post it.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Not sure where the error is coming from

    Quote Originally Posted by Duke_fann View Post
    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.


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  4. #4
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Not sure where the error is coming from

    Quote Originally Posted by jcloud View Post
    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.


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  6. #6
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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 ?

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not sure where the error is coming from

    i=types.length;
    Why don't you try changing the place of this line of code a little down?
    Try to understand the exception: ArrayIndexOutOfBounds

Similar Threads

  1. Exception Coming.... Whats wrong with my code....
    By syehjafa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 28th, 2011, 04:38 AM
  2. Recursion ouput is not coming for value more than 40.
    By Revati in forum Algorithms & Recursion
    Replies: 12
    Last Post: July 7th, 2011, 06:49 PM
  3. Output not coming through properly
    By Camiot in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 16th, 2011, 09:25 PM
  4. Null value coming from Database
    By kosuri.dilip in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 02:03 PM
  5. JFrames not coming up?
    By scooty199 in forum AWT / Java Swing
    Replies: 13
    Last Post: October 30th, 2010, 02:54 AM