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 8124847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:36:09+00:00 2026-06-06T06:36:09+00:00

Question: what is wrong with my arrays, and how do I fix it? Details:

  • 0

Question: what is wrong with my arrays, and how do I fix it?

Details:
I initialized the array in the main method, and the values were set in one method. I called the array values in a 2nd method, and everything was fine.
When I tried to call the array in a 3rd method, I got the out of bounds error, even though the size of the array is exactly the same.
I was trying to call the array in order to copy it, and then sort the 2nd array.

thank you

private static  WeatherLocation[] WeatherSpots = new WeatherLocation[6];
private static Scanner Input = new Scanner(System.in);

public static void main(String[] args) 
{int Count;

for(Count = 0 ; Count < 6; Count++)
    WeatherSpots[Count] = new WeatherLocation();

WeatherSpots[0].LocationID = "Tciitcgaitc";
WeatherSpots[1].LocationID = "Redwood Haven";
WeatherSpots[2].LocationID = "Barrier Mountains";
WeatherSpots[3].LocationID = "Nina's Folly";
WeatherSpots[4].LocationID = "Scooly's Hill";
WeatherSpots[5].LocationID = "Twin Cones Park";
    SetUp();

    String Command = "";
    while(!Command.equals("Quit"))  {
    Menu();

    System.out.print("Enter Command: ");
    Command = Input.nextLine();

    if(Command.equals("Post"))
        PostTemperatureInfo();
    if(Command.equals("Daily"))
        WeeklyReport();
    else if (Command.equals("HighLow"))
        Sorting();
    }
}

public static void PostTemperatureInfo()
{
    Scanner LocalInput = new Scanner(System.in); 
    int K;
    int Temp;
    //...then get the values for each location...

    System.out.println( "Enter the Temperature for each weather station below:\n");
    System.out.println( "---------------------------------------------------------------");

    for(K = 0 ; K < 6 ; K++)  {
        System.out.println( "Weather Station: " + WeatherSpots[K].LocationID);  //Display the location of the fishing spot...

        System.out.print( "Enter Temperature:\t");   //Get the count...
        Temp = LocalInput.nextInt();
         System.out.println( "---------------------------------------------------------------");
         WeatherSpots[K].CatchCount = Temp;
        }
    System.out.println("");
     System.out.println("");
     System.out.println("");
}
public static void WeeklyReport()
{
    for(K = 0 ; K < 6 ; K++) 
        {System.out.println( "" + WeatherSpots[K].LocationID +"\t\t" + WeatherSpots[K].CatchCount + "\t\t" + String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9));
}
}

public static void Sorting()
{int K = 0; 

for(K = 0 ; K < 6 ; K++);
    {int [] copycat = new int[K];


    System.arraycopy(WeatherSpots[K].CatchCount, 0, copycat[K], 0, 6);
    System.out.println("" + copycat[K]);
    Arrays.sort(copycat, 0, K);
    System.out.println("Minimum = " + copycat[0]); 
System.out.println("Maximum = " + copycat[K -1]);  

        }
    }
}
  • 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-06T06:36:11+00:00Added an answer on June 6, 2026 at 6:36 am

    The problem is that you are allocating an array copycat that is only K integers long, and then you are trying to fit 6 elements into it, even when K == 0. I don’t understand your code enough to figure out what the right indexes are, but that’s the source of your problem.

    Actually, I don’t believe that your code as posted will compile. This line from Sorting():

    System.arraycopy(WeatherSpots[K].CatchCount, 0, copycat[K], 0, 6);
    

    seems mighty suspicious. The first and third arguments to System.arraycopy are supposed to be arrays, but copycat[K] is an int. Apparently so is WeatherSpots[K].CatchCount.

    EDIT:

    It seems from your comments and code that the Sorting() routine is just supposed to print the min and max values of WeatherSpots[K].CatchCount. This can be done much more easily than you are doing. Here’s one way:

    public static void Sorting() {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (WeatherLocation loc : WeatherSpots) {
            final int count = loc.CatchCount;
            if (count < min) {
                min = count;
            }
            if (count > max) {
                max = count;
            }
        }
        System.out.println("Minimum = " + min); 
        System.out.println("Maximum = " + max);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure I've worded this question wrong, but I don't know how to explain
sorry ma question went wrong a bit the alis has value like alias =Sri
I may be asking the wrong question here, I'm willing to change it if
Edit: OK I asked the wrong question here. I'm going to be coding a
I might be asking the wrong question here, so I apologize if the answer
QUESTION: What am I missing or doing wrong? I'm trying to migrate fully functional
Quick question, What have I done wrong here. The purpose of this code is
I had a question on a test - I got it wrong, but I
This is an offshoot of this question: Chrome counts characters wrong in textarea with
Ok, i kind of asked the wrong question so I've edited the original question.

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.