Results 1 to 3 of 3
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default What is this asking? I don't really know how to begin.

    I have an exercise for practicing insertion sorts which asks me to do this:

    1) Write an insertion sort in insert the name ‘Sam’ into the list from names.txt

    I know how to write an insertion sort...it's just that I'm not really sure how to integrate inserting an element into the method at the same time.
    This is what I have so far.
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    
    public class exercise4 {
    	static int[] numbers;
    	static String[] words;
    	public static String[] readStringFile(String f)
    			throws FileNotFoundException{
    			List<String> words1 = new ArrayList<String>();
    			Scanner input1 = new Scanner(new File(f));
    			while (input1.hasNext()){
    				words1.add(input1.next());
    			}
    			words = new String[words1.size()];
    			words = words1.toArray(words);
    			  return words;
    			}
    public static void insertionStringSort(String[] words){
    	int i, j;
    	String newInput;
    	 for (j = 0; j < words.length; j++) { //the condition has changed
    		 newInput = words[j];   
    		 i = j - 1;
    		    while (i >= 0) {
    		      if (newInput.compareTo(words[i]) > 0) {//here too
    		        break;
    		      }
    		      words[i + 1] = words[i];
    		      i--;
    		    }
    		    words[i + 1] = newInput;
    		    System.out.println(Arrays.toString(words));
    		  }
    		  System.out.println(Arrays.toString(words));
    		}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,875
    Rep Power
    8

    Default Re: What is this asking? I don't really know how to begin.

    Is that the actual text of the question?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: What is this asking? I don't really know how to begin.

    1) Write an insertion sort in insert the name ‘Sam’ into the list from names.txt
    It seems that all you need to do is append "Sam" to the list of names obtained from names.txt, and then re-sort the list.

Similar Threads

  1. Where to begin?
    By Kennb17 in forum New To Java
    Replies: 5
    Last Post: 06-13-2012, 10:39 AM
  2. Where to begin?
    By Moone in forum New To Java
    Replies: 1
    Last Post: 02-06-2012, 12:58 AM
  3. Where to begin...
    By will.galaxy in forum New To Java
    Replies: 4
    Last Post: 07-06-2011, 03:25 PM
  4. how should i begin..
    By Bgreen7887 in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 09:48 PM
  5. Where do I begin
    By shaani in forum New To Java
    Replies: 1
    Last Post: 12-24-2009, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •