Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:31:23+00:00 2026-06-14T03:31:23+00:00

I have been having constant problems with my code concerning arrays . I received

  • 0

I have been having constant problems with my code concerning arrays .

I received a constant string of errors :

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
at Ass1.genTimeArray(Ass1.java:150)
at Ass1.main(Ass1.java:54)

Here’s my code :

import java.util.* ;
public class Ass1 {


 static int VIX = 0 ; // Traversing Voltage Component in Component Value Array 
 static int CIX = 1 ; // Traversing Capacitor
 static int LIX = 2; // Traversing Inductor
 static int RIX = 3; // Traversing Resistor

 public static void main (String args[]) { 
  System.out.println(

  double[] compArr  = getCircuitComp() ; 

  System.out.println("-----------------------------------------------------") ;
  System.out.println(""); 
  System.out.println("Voltage Source V = " + compArr[VIX]+ "volts") ; 
  System.out.println("Capacitor C = " + compArr[CIX]+ "farads");
  System.out.println("Inductor L = " + compArr[LIX] + " henrys") ; 
  System.out.println("Resistor R =" + compArr[RIX] + "ohms") ; 
  System.out.println("-----------------------------------------------------") ; 

  boolean outerLoopFlag ; 
  boolean innerLoopFlag ; 
  int i ; 
  int n = 1  ; 
  int qArray[] = new int[n] ; 
  int timeArr[] = new int [n] ;
  System.out.println("-----------------------------------------------------") ;
  Scanner input = new Scanner (System.in) ;
  while ( outerLoopFlag = true) { 

   System.out.println("Reset Component Values (y/n) ?") ; 
   String answer = input.nextLine() ; 
   char ans = answer.charAt(0) ;  
   if ( ans == 'y') { 
     getCircuitComp() ; 

   } 
   else { 
     } 
   innerLoopFlag = true ; 
   while ( innerLoopFlag = true){ 
    System.out.print("Run a simulation?") ; 
      String answer2 = input.nextLine() ; 
   char ans2 = answer2.charAt(0) ;  
    if (ans2 == 'y'){

    System.out.println("Enter a maximum time :") ; 
    double tMax = input.nextDouble(); 
    System.out.println("Enter a time step:") ; 
    double tStep = input.nextDouble();
       timeArr[n]= genTimeArray(tMax,tStep,n);  
       qArray[n] = genQArray( timeArr , n , compArr) ;  
       displayQFunction(timeArr,qArray,n );

    }
    else{
     innerLoopFlag = false ;
    }
    System.out.println("-----------------------------------------------------") ; 

    while (innerLoopFlag = false) { 
     System.out.println("Do you want to quit (y/n)?") ;
     String answer3 = input.nextLine() ; 
     char ans3 = answer3.charAt(0) ; 
     if ( ans3 =='y' ){ 
      System.exit(0); 
      } 
     else{}

    }


   } 



 }

}
     ////////////////////////////
 ///*getCircuitComp Method // 

 public static double[] getCircuitComp() {
     Scanner input = new Scanner (System.in) ; 
  boolean flag = true ; 
  double compArr [] = new double[4]; 
     do{ 
   System.out.println("Enter a value V ( 4 to 15):") ; 
   compArr[VIX] = input.nextDouble() ;  
   if ((compArr[VIX] >= 4.0) && (compArr[VIX] <= 15.0)){ 
    flag = false ; 
   }
   else{ 
    System.out.println("Bad Value" + compArr[VIX] ) ;
   }

  }  while (  flag );
  flag = true  ; 
  while (flag) { 
   System.out.println("Enter a value for R ( 5 to 10) :") ; 
   compArr[RIX] = input.nextDouble();  
   if((compArr[RIX] >= 5.0) && (compArr[RIX] <= 10.0)){
flag = false;
   }
   else{
    System.out.println("Bad Value:"+ compArr[RIX]) ; 

   }

  }
   flag = true ; 
   while(flag) { 
    System.out.println("Enter a value for C( 1e-9 to 1e-7 ): ") ; 
    compArr[CIX] = input.nextDouble();  
    if (( compArr[CIX] >= Math.pow(10, -9)) && (compArr[CIX] <= Math.pow(10, -7))){
     flag = false ; 
    }
    else{ 
     System.out.println("Bad Value :"+ compArr[CIX]);
    }


   }
    flag = true ; 
    while(flag){ 
     System.out.println("Enter a value for L(1e-3 to 1e-1) "); 
     compArr[LIX] = input.nextDouble(); 
     if(compArr[LIX]>= Math.pow(10,-3) && compArr[LIX] <= Math.pow(10, -1)){
      flag = false ; 
     }
     else{ 
      System.out.println("Bad Value :" + compArr[LIX]) ; 

     }
    }
    return compArr  ; 
   }


////////////////////////////
///*getTimeArray Method //  
 public static int genTimeArray( double tMax , double tStep , int n ) { 

   int t[] = new int[n] ;
    n = 1 ; 
    t[0] = 0 ; 
    if(t[n] < tMax){
    t[n] = (int) (t[n-1] + tStep) ; 
    n = n + 1 ; 
    }
   return t[n] ;

  } 
  public static int genQArray( int timeArr[] , int n , double compArr [] ) {
   double q[] = new double[n] ; 
   n = 1 ;  
   double s = (compArr[VIX])*(compArr[CIX]) ; 
   double t = Math.exp((-compArr[RIX])/(2*compArr[LIX])); 
   double c = (1)/((compArr[LIX])*(compArr[CIX])) ; 
   double v = Math.pow((compArr[RIX])/(2*(compArr[LIX])),2) ;
   double r = (timeArr[n])*(Math.sqrt(c-v)) ;
   q[n] = (s)*(t)*(Math.cos(r)) ; 

   return (int) q[n] ; 
  }
////////////////////////////
///*displayQFunction Method // 
  public static void  displayQFunction(int[] timeArr , int[] qArray ,int n  ){ 

  n = 1 ;
  System.out.println( timeArr[n]  + qArray[n]) ; 




 }
 }

Could anyone help , I am having a lot of trouble with this code !

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:31:24+00:00Added an answer on June 14, 2026 at 3:31 am

    java.lang.ArrayIndexOutOfBoundsException: 1

    This indicates that you’ve got an array that is less than 2 elements long.

    You’re requesting index 1 (the second element), and Java is saying, “hold on cowboy, this here array’s not long enough.”

    Looks like you need to put some more cows in the corral (that is, populate your array). 🙂


    After a closer look, it looks like the problem starts here

    boolean outerLoopFlag ; 
    boolean innerLoopFlag ; 
    int i ; 
    int n = 1  ;    //  <- Here's a problem
    int qArray[] = new int[n] ; 
    int timeArr[] = new int [n] ;  // <- because your array is now only 1 element long
    

    and continues down here

    double tStep = input.nextDouble();
    timeArr[n]= genTimeArray(tMax,tStep,n);    // <- n is still 1
    

    and continues on to the place where the error happens:

    public static int genTimeArray( double tMax , double tStep , int n ) { 
        int t[] = new int[n] ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having these really odd problems with Visual Studio 2010. At this
I have been having problems with creating a download list of files for a
I have been having constant struggle with Visual Studio debugger and finally got fed
I have been having an ongoing fight with this email setup. The code below
I have been having problems installing VB6 on Windows 7. I realize it is
I have been having some trouble using RapidXML to parse a string. I receive
I have been having a problem lately with my JavaScript CODE and taking a
I have been having some problems trying to get my PHP running. When I
I have been having trouble pulling up a custom UIPickerView from the textfield's inputview
I have been having this annoying problem when trying to implement a picture gallery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.