Jump to content

Java program help?

Johnmakuta

I have to write a program that takes a list of names from the user and then puts them in title case and sorts them alphabetically.

The only catch is that I can't use any Collection methods! 

 

Please Help!

 

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String entered = "";
        ArrayList <String> unsortList = new ArrayList();
        System.out.println("Please enter some names");
        
        while (!(entered.equals("Stop"))) {
            System.out.println("Enter the next name:");
            entered = scan.nextLine();
            entered = titleCase(entered);
            if (entered.equals("Stop")) {
                break;
            }
            unsortList.add(entered);
        }
        
        
        System.out.println(sort(unsortList));
    }

    public static String titleCase(String s) {
        s = s.toLowerCase();
        s = s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
        return s;
    }
    
    
    public static ArrayList <String> sort(ArrayList <String> toSort) {
      ArrayList <String> holder = new ArrayList();
      ArrayList <String> holder_2 = new ArrayList();
      ArrayList <String> sorted = new ArrayList();
      for (String ecx: toSort) {
        holder.add(ecx);
      }
      for (String ecx_2: toSort) {
        holder_2.add(ecx_2);
      }
      int c = 0;
      for (String test: holder) {
        for (int i = 0; i < holder_2.size(); i++) {
          if ( (test.compareTo(holder_2.get(i))) > 0 ) {
            test = holder_2.get(i);
          }
        }
        sorted.add(test);
        holder_2.remove(test);
        c++;
      }
      
      
      return sorted;
    }
    
}

And time is of the essence so please help!!!!!!!!!!

 

 

Main_2.java

 

 

 

 

 

 

 

 

I FIGURED IT OUT. NEVERMIND

Edited by Johnmakuta
I figured it out!
Link to comment
Share on other sites

Link to post
Share on other sites

Is title case is that every word is started by uppercase letter? If that's true then you just uppercase first letter of word, and if your names will be one word then it is fine, but if there is two words then only first be uppercase second will be lowercase.

 

Second thing is, I think what you're struggling with is sorting? You said you cannot use objects method, but you mean only sorting or compareTo counts to object method that you are not allowed to use?

 

You're creating a loot of new arrays in your sorting function, I see you remove some elements and add some. Sorting can be done just on original array. And I think you want use just set, and get on it, if you can use compareTo to compare two strings lexicographically then you would be home with some easy bubble sort or insert sort.

Link to comment
Share on other sites

Link to post
Share on other sites

	/* Sorts toSort by selection */
	public static ArrayList <String> sort(ArrayList <String> toSort)
	{
		int i, j, jmin;
		
		for(i = 0; i < toSort.size() - 1; i++)
		{
			jmin = i;
			
			for(j = i + 1; j < toSort.size(); j++)
				if(isFirst(toSort.get(j), toSort.get(jmin)))
					jmin = j;
			
			Swap(toSort, i, jmin);
		}
		
		return toSort;
	}
	
	private static void Swap(ArrayList<String> toSort, int i0, int i1)
	{
		String temp = toSort.get(i0);
		toSort.set(i0, toSort.get(i1));
		toSort.set(i1, temp);
	}
	
	public static boolean isFirst(String s1, String s2)
	{
		int minLen = Math.min(s1.length(), s2.length());
		
		for(int i = 0; i < minLen; i++)
		{
			if(s1.charAt(i) < s2.charAt(i))
				return true;
			else if(s1.charAt(i) > s2.charAt(i))
				return false;
		}
		return false;
	}

Try this, I used the Selection Sorting.

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, FedeWar said:

	/* Sorts toSort by selection */
	public static ArrayList <String> sort(ArrayList <String> toSort)
	{
		int i, j, jmin;
		
		for(i = ; i < toSort.size() - 1; i++)
		{
			jmin = i;
			
			for(j = i + 1; j < toSort.size(); j++)
				if(isFirst(toSort.get(j), toSort.get(jmin)))
					jmin = j;
			
			Swap(toSort, i, jmin);
		}
		
		return toSort;
	}
	
	private static void Swap(ArrayList<String> toSort, int i0, int i1)
	{
		String temp = toSort.get(i0);
		toSort.set(i0, toSort.get(i1));
		toSort.set(i1, temp);
	}
	
	public static boolean isFirst(String s1, String s2)
	{
		int minLen = Math.min(s1.length(), s2.length());
		
		for(int i = ; i < minLen; i++)
		{
			if(s1.charAt(i) < s2.charAt(i))
				return true;
			else if(s1.charAt(i) > s2.charAt(i))
				return false;
		}
		return false;
	}

Try this, I used the Selection Sorting.

Why not just use String#compareToIgnoreCase? It returns 

Quote

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

So instead, you could have the inner condition being:

if (toSort.get(j).compareToIgnoreCase(toSort.get(jmin)) < ) {
	jmin = j;
}

 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Blade of Grass said:

Why not just use String#compareToIgnoreCase? It returns 

So instead, you could have the inner condition being:


if (toSort.get(j).compareToIgnoreCase(toSort.get(jmin)) < ) {
	jmin = j;
}

 

Yeah it isn't a collection method so it should be correct.

Link to comment
Share on other sites

Link to post
Share on other sites

include this code as a separate library then reference the release function() to find the values you need... let me know if it works... i've had these same issues before too... not fun

 

var fs = require( "fs" );

module.exports = function( Release ) {

	var
		files = [
			"dist/jquery.js",
			"dist/jquery.min.js",
			"dist/jquery.min.map",
			"dist/jquery.slim.js",
			"dist/jquery.slim.min.js",
			"dist/jquery.slim.min.map",
			"src/core.js"
		],
		cdn = require( "./release/cdn" ),
		dist = require( "./release/dist" ),
		ensureSizzle = require( "./release/ensure-sizzle" ),

		npmTags = Release.npmTags;

	Release.define( {
		npmPublish: true,
		issueTracker: "github",
		/**
		 * Ensure the repo is in a proper state before release
		 * @param {Function} callback
		 */
		checkRepoState: function( callback ) {
			ensureSizzle( Release, callback );
		},
		/**
		 * Set the version in the src folder for distributing AMD
		 */
		_setSrcVersion: function() {
			var corePath = __dirname + "/../src/core.js",
				contents = fs.readFileSync( corePath, "utf8" );
			contents = contents.replace( /@VERSION/g, Release.newVersion );
			fs.writeFileSync( corePath, contents, "utf8" );
		},
		/**
		 * Generates any release artifacts that should be included in the release.
		 * The callback must be invoked with an array of files that should be
		 * committed before creating the tag.
		 * @param {Function} callback
		 */
		generateArtifacts: function( callback ) {
			Release.exec( "grunt", "Grunt command failed" );
			Release.exec(
				"grunt custom:-ajax,-effects,-deprecated --filename=jquery.slim.js && " +
					"grunt remove_map_comment --filename=jquery.slim.js",
				"Grunt custom failed"
			);
			cdn.makeReleaseCopies( Release );
			Release._setSrcVersion();
			callback( files );
		},
		/**
		 * Acts as insertion point for restoring Release.dir.repo
		 * It was changed to reuse npm publish code in jquery-release
		 * for publishing the distribution repo instead
		 */
		npmTags: function() {

			// origRepo is not defined if dist was skipped
			Release.dir.repo = Release.dir.origRepo || Release.dir.repo;
			return npmTags();
		},
		/**
		 * Publish to distribution repo and npm
		 * @param {Function} callback
		 */
		dist: function( callback ) {
			cdn.makeArchives( Release, function() {
				dist( Release, files, callback );
			} );
		}
	} );
};

module.exports.dependencies = [
	"archiver@0.14.2",
	"shelljs@0.2.6",
	"npm@2.3.0",
	"chalk@1.1.1"
];

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, canadiansteve2 said:

-snip-

His question is about Java, not JavaScript....

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×