CVGalleryGamesPreviewRandomJavaSiteWeb
Site updates
Testing New System now...

String Comparison

This program demonstrates Vectors, String comparison and menus. It allows you to input a number of strings, list the strings you have inputted, and compare them. It will show the difference between using == and equals() to compare objects.

Download:
CompareString.java - command line
CompareString.class - command line
import java.util.*;   Needed for Vectors
import java.io.*;     Needed for inputs

public class CompareString
{
    static Vector strings = new Vector();
 
    static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    
    public static void main(String[] args)
    {
	while(true)
	    {
		System.out.println("1) Add new string");
		System.out.println("2) List strings");
		System.out.println("3) Compare two strings");
		System.out.println("4) Quit");
		System.out.println();
		
		int chose = getNumber(1,4);
		
		if(chose == 1)
		    add();
		
		if(chose == 2)
		    list();
		
		if(chose == 3)
		    compare();

		if(chose == 4)
		    System.exit(0);

	    }
    }

    private static void add()
    {
	System.out.println();
	System.out.println("Enter new string");

	System.out.print("?");
	try
	    {
		strings.add(input.readLine());
	    }
	catch(IOException e)
	    {
		System.out.println(e);
	    }
    }

    private static void list()
    {
	for(int i=0; i<strings.size(); i++)
	    {
		System.out.println( i + ": " + strings.elementAt(i));
	    }

	System.out.println();
    }

    private static void compare()
    {
	int first, second;
	String sfirst , ssecond;

	System.out.println("Index of first string");
	first = getNumber(0, strings.size()-1);

	System.out.println("Index of second string");
	second = getNumber(0, strings.size()-1);

	sfirst = (String)strings.elementAt(first);
	ssecond = (String)strings.elementAt(second);

	System.out.println();
	System.out.println("Comparison with == : " + (sfirst==ssecond));
	System.out.println("Comparison with equals() : " + sfirst.equals(ssecond));
	System.out.println();
    }

    private static int getNumber(int min, int max)
    {       
	boolean notyet = true;
	while(notyet)
	    {
		System.out.print("?");
		String s = new String("");
		
		try
		    {
			s = input.readLine();
		    }
		catch(IOException e)
		    {
			System.out.println(e);
		    }
		
		int i;

		try
		    {
			i = Integer.parseInt(s);
			
			if(i<=max && i>=min)
			    {
				return i;
			    }
			else
			    {
				System.out.println("Not an option");
			    }
		    }
		catch(NumberFormatException e)
		    {
			System.out.println("Not an option");
		    }
		
		
	    }
	//never here;
	return -128; This is never reached because of the while loop above
	
    }

}
	 
	 
NOTE: The Java section is mostly in maintenence mode. I don't have time to work on it right now. Errors will be corrected if pointed out, but they are not actively being searched for. Newer site features, like alternate stylesheets, may cause problems with these pages.