Results 1 to 6 of 6
  1. #1
    jnjh is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Not getting why the program is not running

    Greetings,

    I was trying to write a program which takes in the array of size 18, calculates the sum from 0th to 14th item and stores in the 15th place. counts the avg and stores in 16th... and finds the min item in the array and stores it in 17th....

    I have written the code like this..



    import java.util.Arrays;
    public class Ex {
    public static void main(String args[]){

    int[] array =new int[] {3,2,4,5,6,4,5,7,3,2,3,4,7,1,2,0,0,0};

    for(int i=array[0];i<=array[14];i++){
    int res = array[i]+array[i+1];
    res = array[15];
    System.out.println(array);
    for(int j=array[0];j<=array.length;j++){
    float avg = (array[j]/18);

    avg = array[16];

    Arrays.sort(array);
    int min = array[0];
    min = array[17];
    System.out.println("The smallest No in the array is" +min);
    }
    }
    }


    }

    I am very new to java.... kindly help me...

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Does the code compile? If not and you can't understand the compiler messages, post them.

    If it does compile but does not behave at runtime as you expect say what it *does* do (including any runtime exceptions) as well as what you were expecting.

  3. #3
    jnjh is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    I am using Eclipse ganymede.. as soon as i hit the run button it is neither complies nor runs ... it is just giving a msg in console that <terminated> Ex [java application] D:/jdk1.6/bin/javaw.exe...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jnjh View Post
    Java Code:
    import java.util.Arrays;
    public class Ex {
    	public static void main(String args[]){
    	 
    		int[] array =new int[] {3,2,4,5,6,4,5,7,3,2,3,4,7,1,2,0,0,0};
    		
    		for(int i=array[0];i<=array[14];i++){
    So variable 'i' starts with a value 3 (array[0]) and while it is less or equal than 2 (array[14]) the loop runs; given those two values the body of the loop never runs so nothing is printed (all your System.out.println( ... ) calls are in the body of the loop).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jnjh is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    ya ya sorry.... but is there is any other way to iterate from 0th element to 14th element...

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, how many items would you have to iterate to go from 0-14? Set your loop to run that many times. Also, check out what fairly general for loops look like with google.

Similar Threads

  1. Help with running the program :)
    By dj_ee3 in forum New To Java
    Replies: 2
    Last Post: 02-28-2011, 12:19 AM
  2. Running Java program
    By Natalia in forum New To Java
    Replies: 5
    Last Post: 11-27-2010, 10:52 PM
  3. hi the program is compiling but not running why
    By javastuden in forum New To Java
    Replies: 6
    Last Post: 09-21-2010, 10:14 AM
  4. Running a program in a JPanel
    By ttelloow in forum Advanced Java
    Replies: 5
    Last Post: 07-26-2010, 07:18 PM
  5. Program running indefinitely
    By bayan in forum New To Java
    Replies: 2
    Last Post: 04-27-2010, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •