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

Thread: NEED HELP NOW, DUE TODAY

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

    Default NEED HELP NOW, DUE TODAY

    I keep getting the same error "java.lang.ArrayIndexOutOfBoundsException:5" in my code. Ive read it means i have an array value that doesnt exist used. But i dont see it. Can someone see my code and tell me whats wrong. It basically prompts user to enter three values. Then uses them, makes a table, and now im trying to do a ZERO FINDER with my iff statements. Can someone look it over, happens at line 40.


    import java.util.Scanner;
     
    public class locZero{
    	public static void main(String[]args){
     
    		Scanner input= new Scanner(System.in);
     
    		double minX, maxX;
    		int numPoints;
    		int numChange=0;
     
    		System.out.print("Enter number of points: ");
    		numPoints=input.nextInt();
    		System.out.print("Enter min x: ");
    		minX=input.nextDouble();
    		System.out.print("Enter max x: ");
    		maxX=input.nextDouble();
     
     
    		double [] x= new double[numPoints];
    		double [] y= new double[numPoints];
    		int [] change= new int[numPoints];
     
    		System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
     
    		for(int i=0;i<numPoints;i++){
    			System.out.print(i+"\t");
     
    			x[0]=minX;
    			x[numPoints-1]= maxX;
    			x[i]=x[0]+i*((maxX-minX)/(numPoints-1));			
    			y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
     
     
    			System.out.print(x[i]);
    			System.out.print("\t");
    			System.out.print(y[i]);
     
    			for(i=0;i<=numPoints;i++){
    				if(y[i]>=0 && y[i+1]<0){
    				change[numChange]=i;
    				numChange++;
    				}
    				else if(y[i]>0 && y[i+1]<=0){
    				change[numChange]=i;
    				}
    				numChange++;
    				}
     
    			System.out.print("\t"+change[i]);
    			System.out.println();
     
    		}
     
     
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NEED HELP NOW, DUE TODAY

    It helps to post the full stack trace - which tells what line the exception is being thrown, and should point you in the right direction. I'd suggest reading this link: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    add some System.out.println statements to debug and print out the values of the indexes you are trying to access, and look at those loops carefully.

    Also, suggested reading: Ease Up at JavaRanch

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NEED HELP NOW, DUE TODAY

    It should read like a table.
    First producing the I's, then x[i]'s which range depending on your min and max, and then y[i] using the forumla in terms of x's.

    Lastly, and this is where my error occurs. Im trying to find where the numbers have to Cross ZERO, or basically what my If statments say. But its not working. Help?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NEED HELP NOW, DUE TODAY

    Did you read read my last post? None of what you just said suggests otherwise, and stating 'its not working' is fairly meaningless - what's not working? Whats the behavior? Stack trace? Output (expected and received)? Suggested reading: How To Ask Questions The Smart Way

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: NEED HELP NOW, DUE TODAY

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NEED HELP NOW, DUE TODAY

    Your inner for loop should probably not be reseting "i", you should probably choose a different variable name such as "j".


    double lastY = 0;
     
    for(int i=0;i<numPoints;i++){
       System.out.print(i+"\t");
       x[0]=minX;		
       x[numPoints-1]= maxX;
     
     
       x[i]=x[0]+ i*((maxX-minX)/(numPoints-1));
       y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
     
       System.out.print(i);
       System.out.print("\t");
       System.out.print(x[i]);
       System.out.print("\t");
       System.out.println(y[i]);
     
       //i gets reset here
       for(i=0;i<numPoints;i++){
          if (y[i]>=0 && y[i+1]<0){
                       ...
     
       }//for
    }//for

    Also "y[i+1]", will be null since it hasn't yet been populated with a value. The OutOfBounds exception is occuring when you reach the next to last interation in "y[i+1]". If numPoints = 5, then plugging the values in during the next to last iteration we see:

    for (i=0; i<=5;i++) { 
        if (y[5] >=0 && y[5+1]<0) ...

    y[5+1]" also referred to as "y[6]" doesn't exist. Only y[0], y[1], y[2], y[3], y[4], y[5] exist. You could probably eliminate the inner for loop and just keep track of the last y[i] value and compare it to the current y[i] value. Try 1 of the following two code examples:

    Since the function appears linear (and a linear function will only cross 0 once) you could do the following:
    Scanner input= new Scanner(System.in);
     
    double minX =0.0;
    double maxX=0.0;
    int numPoints=0;
     
    double lastX=0.0;
    double lastY=0.0;
     
    System.out.print("Enter number of points: ");
    numPoints=input.nextInt();
    System.out.print("Enter min x: ");
    minX=input.nextDouble();
    System.out.print("Enter max x: ");
    maxX=input.nextDouble();
     
    System.out.println("Numpoints: " + numPoints);
    double [] x= new double[numPoints];
    double [] y= new double[numPoints];
    int change=0;
    System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
     
    for(int i=0;i<numPoints;i++){
       System.out.print(i+"\t");
       x[0]=minX;		
       x[numPoints-1]= maxX;
     
     
       x[i]=x[0]+ i*((maxX-minX)/(numPoints-1));
       y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
     
       System.out.print(i);
       System.out.print("\t");
       System.out.print(x[i]);
       System.out.print("\t");
       System.out.println(y[i]);
     
      //go to else if for first iteration
       if (i>0){
           if(lastY >=0 && y[i]<=0){
              //System.out.println("Inside inner if");
              change=i;
           }//if
          else if(lastY<= 0 && y[i]>=0){
             //System.out.println("Inside inner else if");
             change=i;
         }//else if
      }//if
     
      //in case the first number starts on 0
      else if(y[i]==0){                           
        //System.out.println("Inside inner if");
       change=i;
      }//else if
     
      //this will print out the value of change during every iteration,
      //probably not what you want
      //if you only want to print it out once, move it to the outside of the "for" loop
      //since the function appears linear it will only cross the "y" axis 1 time,
      //then you don't need to use an array, you could just use  "double change;"
      //and eliminate "numChange"
      System.out.print("\t"+change);
      System.out.println();
     
      //set lastY=y[i]
       lastY = y[i];
     
    }//for

    Otherwise, you can do the following for both linear and non-linear functions (which may cross the y axis more than once):
    Scanner input= new Scanner(System.in);
     
    double minX =0.0;
    double maxX=0.0;
    int numPoints=0;
    int numChange=0;
     
    double lastX=0.0;
    double lastY=0.0;
     
    System.out.print("Enter number of points: ");
    numPoints=input.nextInt();
    System.out.print("Enter min x: ");
    minX=input.nextDouble();
    System.out.print("Enter max x: ");
    maxX=input.nextDouble();
     
    System.out.println("Numpoints: " + numPoints);
    double [] x= new double[numPoints];
    double [] y= new double[numPoints];
    int [] change= new int[numPoints];
     
    System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
     
     
    System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
     
    for(int i=0;i<numPoints;i++){
       System.out.print(i+"\t");
       x[0]=minX;		
       x[numPoints-1]= maxX;
     
     
       x[i]=x[0]+ i*((maxX-minX)/(numPoints-1));
       y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
     
       System.out.print(i);
       System.out.print("\t");
       System.out.print(x[i]);
       System.out.print("\t");
       System.out.println(y[i]);
     
      //go to else if for first iteration
       if (i>0){
           if(lastY >=0 && y[i]<=0){
              //System.out.println("Inside inner if");
              change[numChange]=i;
              numChange++;
           }//if
          else if(lastY<= 0 && y[i]>=0){
             //System.out.println("Inside inner else if");
             change[numChange]=i;
     
            //added numChange++
            numChange++;
         }//else if
      }//if
     
      //in case the first number starts on 0
      else if(y[i]==0){                           
        //System.out.println("Inside inner if");
       change[numChange]=i;
       numChange++;
      }//else if
     
      //we will print out the crossing points outside of this for loop
      // System.out.print("\t"+change[0]);
      //System.out.print("Outside inner for \t"+change[numChange]);
      System.out.println();
     
      //set lastY=y[i]
       lastY = y[i];
     
    }//for
     
    System.out.print("The function crosses the y-axis at the following i value(s):");
    System.out.print("\t");
    for (int j=0; j<numChange;j++){
     
       if (j>0){
          System.out.print(", " + change[j]);
       }//if
       else{
          System.out.print(change[j]);
       }//else
    }//for
     
    System.out.println();                
    System.out.println();
    Last edited by cgeier; March 21st, 2011 at 07:15 PM.

Similar Threads

  1. Replies: 0
    Last Post: March 14th, 2010, 07:33 AM
  2. [SOLVED] Help me with different activities in Java program
    By xyldon27 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 9th, 2009, 09:42 AM