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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:07:51+00:00 2026-05-26T10:07:51+00:00

I’m having some problems trying to figure out how to insert an integer using

  • 0

I’m having some problems trying to figure out how to insert an integer using a scanner into an ArrayList. I’m not that great (actually not even really good) at java but I’m just trying to figure some things out and any help would be great.

package mySort;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;



public class MergeInsert {
private int limit = 100;
//private int size = 0;
private ArrayList<Integer> ArrayToSort;

public MergeInsert(int x) {
    ArrayToSort = new ArrayList<Integer>(x);
    }

public MergeInsert(Scanner integerScan){
    int j = 0;
    while(integerScan.hasNextInt()){
        this.insert(integerScan.hasNextInt());
        if (j % 10000 == 0){
            long time = System.nanoTime();
            System.out.println(j + "," + time);
        }
    }
}

public void insert(int x){
    for(int i=0; i<ArrayToSort.size(); i++){
        ArrayToSort(size++) = x;
    }
}


//  public MergeInsert(int v){
//      int val = v;
//  }

//    public void insertFile(){
//      try {
//          Scanner integerScan = new Scanner(new FileInputStream(""));
//          while(integerScan.hasNextInt()){
//              new MergeInsert(integerScan.nextInt());
//          }
//      }
//       catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
//    }


public void sort(){

}

public void mergeSort(ArrayList<Integer> in, int low,int high){
    int n = in.size();
    int mid = (high+low)/2;
    if (n<2){  //already sorted
        return;
    }
    if ((high - low) < limit){
        insertionSort(in);
    }
    ArrayList<Integer> in1 = new ArrayList<Integer>(); //helper
    ArrayList<Integer> in2 = new ArrayList<Integer>(); //helper
    int i=0;

    while (i < n/2){ //moves the first half to the helper
        in1.add(in.remove(0));
        i++;
    }
    while (!in.isEmpty()) //moves the second half to the helper
        in2.add(in.remove(0));
    mergeSort(in1, low, mid); //breaks it down some more like mergesort should
    mergeSort(in2, mid+1, high); //does it again
    merge(in1,in2,in); //trying to build it up again
    }

public void merge(ArrayList<Integer> in, ArrayList<Integer> in1, ArrayList<Integer> in2){
    while (!in1.isEmpty() || !in2.isEmpty()) //as long as both helpers still have elements
        if ((in1.get(0).compareTo(in2.get(0)) <= 0)) //comparison to rebuild
            in.add(in1.remove(0)); //building it back up
        else
            in.add(in2.remove(0)); //still building
    while(!in1.isEmpty()) //as long as the first helper isn't empty keep building
        in.add(in1.remove(0));
    while(!in2.isEmpty()) //as long as the second helper isn't empty keep building
        in.add(in2.remove(0));
}

public ArrayList<Integer> insertionSort(ArrayList<Integer> in){
    int index = 1;
    while (index<in.size()){
        insertSorted((int)(in.get(index)),in,index);
        index = index +1;
    }
    return in;
}

public ArrayList<Integer> insertSorted(Integer s, ArrayList<Integer> in, int index){
    int loc = index-1;
    while((loc>=0) || s.compareTo(in.get(loc)) <= 0){
        in.set(loc + 1, in.get(loc));
        loc = loc -1;
    }
    in.set(loc+1, s);
    return in;
    }


/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {
    Scanner integerScan = new Scanner(new FileInputStream("src/myRandomNumbers.txt"));
    MergeInsert myObject = new MergeInsert(integerScan);
    myObject.sort();

}

}

It’s not completely finished but the idea behind all of this is to try and improve on MergeSort. Basically once the elements get broken down to a certain point cut to InsertionSort because it is usually better on really small (really small being relative) sets of data.

  • 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-26T10:07:51+00:00Added an answer on May 26, 2026 at 10:07 am
    public void insert(int x){
        ArrayToSort.add(x); // add it to the end
    }
    

    The reason is… even if you go

    ArrayToSort = new ArrayList<Integer>(100000);
    

    It still has a size of 0. It just has a CAPACITY of 100000.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and

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.