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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:36:21+00:00 2026-05-25T20:36:21+00:00

I have tried to print my array using a few different methods and seem

  • 0

I have tried to print my array using a few different methods and seem to have screwed it all up. I used to get something back out of it and now I can only get zero’s back out. If anybody knew a way to print the array, easily, that would be great. Using Array.toString doesn’t seem to work for some reason.

    public class Array {
private long[] InitialArray;
private int size = 0;

/**
 * the default constructor
 */
public Array(int size){
    InitialArray = new long[size]; 
}

/**
 * this will scan in integers from a file
 * @param scans
 */
public Array(Scanner scans){
    this(1);
    int j = 0; //just used as a counter basically
    while(scans.hasNext()){ //grabs each element
        this.insert(Integer.parseInt(scans.nextLine())); //inserts it
        j++; //steps through using the counter
    }
}

/**
 * this will make the array longer if more space is needed so you don't
 * crash if you have a very large set of numbers
 */
public void stretch(){
    long[] temp = new long[InitialArray.length *2]; //makes a temp array double the normal size
    for(int i=0; i < InitialArray.length; i++)
        temp [i] = InitialArray[i]; //steps through the old array to keep the data
    InitialArray = temp; //assigns the temp array to the new array
}

/**
 * this will insert each element read, from a file, into the array
 * @param x
 */
public void insert(int x){
    if (this.size+1 == InitialArray.length){ //trying not to crash
        stretch(); //making sure I don't crash
    }
    InitialArray[size++] = x; //stepping through the numbers and inserting
    sorter();
}

/**
 * this is a selection sort on the array
 * It's not the quickest, but it's fairly easy to build and understand
 * Just finds the minimum element and swaps it to it's proper place
 * @return 
 */
public long[] sorter(){
    for (int i=0; i<InitialArray.length-1; i++){
        int minimum = i; //assigning a minimum
        for (int j=i+1; j<InitialArray.length; j++){
            if (InitialArray[minimum] > InitialArray[j]) { //checking to make sure it is the smallest
                minimum = j; //if it isn't then do this
            }
        }
        if (minimum != i){ //all the swapping stuff, which I never really understand but it works
            long temp = InitialArray[i];
            InitialArray[i] = InitialArray[minimum];
            InitialArray[minimum]= temp;
        }
    }
    return InitialArray;
}
/**
* @param args
  */
public static void main(String[] args) {
Scanner scans;
try {
    scans = new Scanner(new FileInputStream("src/some numbers.txt"));
    Array InitialArray = new Array(scans);
    System.out.println(InitialArray);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
}

I kept getting an error saying it has to be an array type but resolved to Array. I just need it to see if anything else is even working.

  • 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-05-25T20:36:21+00:00Added an answer on May 25, 2026 at 8:36 pm

    Answer to what’s actually going on is below the rule. Presumably this is only code for learning about Java – as otherwise the ArrayList class would be more suitable. You should try to use this as an exercise in debugging… what would help you to work out what was going wrong (e.g. logging, stepping through with a debugger)? How can you tackle one problem at a time? How could you unit test the code? That way you’ll learn more than just what was wrong with your specific code.


    You haven’t overridden toString in the Array class. If you add this method:

    @Override public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                builder.append(" ");
            }
            builder.append(InitialArray[i]);
        }
        return builder.toString();
    }
    

    then it prints values. It doesn’t quite print the values you were expecting, due to the sorter method, but that’s a separate problem.

    Currently your sorter() method – which may or may not actually be sorting correctly, I haven’t checked – sorts the whole of the array, not just up to size. That means you end up sorting your “valid” values into the “invalid” part of the array which you then overwrite later.

    If you just change your limits to use size instead of InitialArray.length it seems to work – but as I say, I haven’t checked the sorting itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried using fopen, file-get-contents and a curl request but I always get
i've tried a few different json methods (stringify, toJSON, and probably some totally irrelevant
I need to create an array using a object using different format/structure I have:
I have tried to find how to create DLL-s on linux using google, but
I have tried different examples to filter a gridview by dropdownlist, but is it
I'm currently using this function to get all my contacts from Google Contacts. session_start();
I have a method that get's all devices that share a specific ID. Foreach
Previously I have only used the Scala templates, but now am I using Java
I have tried <ul id=contact_list> <li id=phone>Local 604-555-5555</li> <li id=i18l_phone>Toll-Free 1-800-555-5555</li> </ul> with #contact_list
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is

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.