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 3 of 3

Thread: Hi..i am new to java. i am facing an exception error while running loop with int and double input

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi..i am new to java. i am facing an exception error while running loop with int and double input

    Hi,
    Please find below the requirement details:
    1)Enter the number of carriers:
    2(say)
    2)Enter the details of carrier1:
    15(amount of time taken to deliver an item
    125.50(cost to deliver an item
    3)Enter the details of carrier2:
    12
    150.50

    But when i am trying to run the loop, the code prints till the integer details of carrier2 and while going to the double details arrayindexoutofbound exception is generated.

    Code:

    public class sample{
    int a;
    public static void main(String args[])
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of carriers");
    a = sc.nextInt();
    int[] b = new int[];
    double[] c = new Double[];
    System.out.println("Enter the carriers details:");
    for(i=0;i<a;i++){
    b = sc.nextInt();
    c = sc.nextDouble();
    }

    But when i run the code for say a=2, instead of printing the double value of 2nd carrier arrayindexoutofbound error is displayed.

    Can you please let me know where i am getting wrong in the concept?

    Thanks in advance.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    180
    My Mood
    Asleep
    Thanks
    5
    Thanked 13 Times in 13 Posts

    Default Re: Hi..i am new to java. i am facing an exception error while running loop with int and double input

    I don't think your app is runnable.
    Whatever you are, be a good one

  3. #3
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi..i am new to java. i am facing an exception error while running loop with int and double input

    a int has to go up to at most 15 iterations at a =2. b = 15. sc object scanner must pick up to 15 iterations. variable a, b, sc.

    int a=15; //notice b array object starts at b[0] index. The loop is now inbound of b[a] index.

    for(int i=0; I > = 15; I ++)
    Last edited by planetHollywood; January 15th, 2018 at 11:28 PM.

Similar Threads

  1. Facing OutOfMemoryError Java Heap Space error
    By kodamv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 21st, 2014, 01:18 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Loop & custom exception error Please help
    By dpjmie in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 18th, 2013, 04:40 PM
  4. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 10:00 PM
  5. Exception error display with one input and not the other
    By worldchamp in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 4th, 2012, 12:45 PM

Tags for this Thread