My program is not case sensitive it does not display the output my proff wants PLEASE HELP! I don't know how to make it case sensitive


The output should be:
As entered
0:a:apple
1:a:Apple
2:z:Zone
3:a:apple
Bubble Sorted
0:a:apple
1:a:Apple
3:a:apple
2:z:Zone
Selection Sorted
1:a:Apple
0:a:apple
3:a:apple
2:z:Zone

My program does:
As entered
0:a:apple
1:a:Apple
2:z:Zone
3:a:apple
Bubble Sorted
1:a:Apple
2:z:Zone
0:a:apple
3:a:apple
Selection Sorted
1:a:Apple
2:z:Zone
0:a:apple
3:a:apple

Here is my code

package javaapplication9;

import java.util.*;

/**
 *
 * @author www.homeworkjava.com
 */
public class Main {
    static int i = 1;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
        

        String anArray[][];

        anArray = new String[100][100];
        String data;
        System.out.println("Enter data: "); //ask for data
        data = in.next();
        anArray[0][0] = data;
        anArray[0][1] = data.substring(0, 1);


        while (!data.equalsIgnoreCase("done"))
        {

             System.out.println("Enter data: ");
             data = in.next();
             if (data.equalsIgnoreCase("done")){
                 break;
             }
          
             else {
             anArray[i][0]= data;
             anArray[i][1]= data.substring(0, 1);
             i++;
             }
            
        }

       System.out.println("As entered");
       for (int j = 0; j < i; j++){
           String f = String.valueOf(j);
           anArray[j][2] = f;
           System.out.println(j + ":" + anArray[j][1].toLowerCase()+ ":" + anArray[j][0]); //print result

       }
       bubblesort(anArray); //call bubble sort
       selectionSort(anArray); //call selection sort
       
    }
//bubble sort method
    static void bubblesort(String Array[][]){
        String datae = null;
        String temps;
        String tempse;
        
        	
	for(int x=1;x<i;x++)
		{
		for(int y=0;y<i-x;y++)
			{
			if(Array[y][1].compareTo(Array[y+1][1])>0)
				{
				temps=Array[y][1];
				Array[y][1]=Array[y+1][1];
				Array[y+1][1]=temps;

                                tempse=Array[y][0];
				Array[y][0]=Array[y+1][0];
				Array[y+1][0]=tempse;

                                tempse=Array[y][2];
				Array[y][2]=Array[y+1][2];
				Array[y+1][2]=tempse;
				
				}
				}
				}

      System.out.println("Bubble Sorted");
       for (int z = 0; z < i; z++){
           datae = Array[z][0];
           Array[z][1]= datae.substring(0, 1).toLowerCase();
           System.out.println(Array[z][2] + ":" + Array[z][1]+":" +Array[z][0]);


       }
    }

    //selection sort method
    static void selectionSort(String arrays[][])
	    {
        String data = null;
	        for (int e = 1; e < i; e++) {

	            // find the index of the ith smallest value
	            int s = e-1;
	            for (int j = e; j < i; j++) {
	                if (arrays[j][0].compareTo(arrays[s][0]) < 0) {
	                    s = j;
	                }
                      
	            }

	            // swap the ith smallest value into entry i-1
	            String temp = arrays[e-1][0];
	            arrays[e-1][0] = arrays[s][0];
	            arrays[s][0] = temp;
                    
                    temp = arrays[e-1][2];
	            arrays[e-1][2] = arrays[s][2];
	            arrays[s][2] = temp;


	        }

                System.out.println("Selection Sorted");
       for (int z = 0; z < i; z++){
           data = arrays[z][0];
           arrays[z][1]= data.substring(0, 1).toLowerCase();
           System.out.println(arrays[z][2] + ":" + arrays[z][1]+":" +arrays[z][0]);

       }

	    }
}

Recommended Answers

All 13 Replies

The output you've posted is hard to understand.
Can you show the ordering you would like for the characters: a A b B c C 1 2

I believe A a B b C c

But beats me the proff is confusing me also...

can you try editing my code to look like the output given?

To get that ordering you'll need to write your own compare() method.
The normal order for characters is: ABC...Zabc...z. All uppercase followed by the lowercase.

Wow i have NO idea how to write my own method of such -,-

What advice does your prof give? He must know he is requiring you to do this.

If he says he really expects you to write your own compare, what course material has he covered that would show you how to do it?
Its not hard to write. It takes less than 20 lines of code.


BTW is this the same problem as was posted (and removed) http://www.javaprogrammingforums.com/whats-wrong-my-code/4693-bubble-sort-selection-sort-strings.html#post14106

Yes it is same problem.

and he did not show us :-/

What material have you covered in your course that you can use to solve this problem?
I need to know to so I don't do something that you haven't studied yet.

We have gone through everything i used in my coding lol... just try it and i'll see if its way too complicated please & thanks

Ok. We'll try to write a compare method with you. First write one to replace the one that you are using now. Then change it to sort with the ordering that you want.

Write a method that takes two strings: S1 and S2 as input and returns an int: greater than 0 if S1 comes after S2, 0 if s1 == S2 and less than 0 if S1 comes before S2.
The method will take the Strings one char at a time in a loop from each string (Assume strings are the same length to begin with) and subtracts the char from S2 from the char
from S2. If the results don't equal 0 it returns the results. If equal 0 it goes to the next characters and tests again. If all are zero the method returns 0.
For testing: Write a very simple short program that passes two strings short same length strings to the method and prints out the results. Use this program to test all three cases.

I am attempting to do this but How do i check if s1 comes after s2?

Compare the characters one by one. As soon as a pair of characters being compared are not equal return the results of the compare.

if s1 comes after s2

s1 comes after s2 if its letters are greater in value than the letters of s2.

If you don't use the standard ASCII ordering for the letters, you will need a way to assign values to each letter so that the ones that come first are lower in value than the ones that come later.

I wrote this but i don't know what to do from there

int method(String s1, String s2){
        int num = 0;
        int num1 = 0;
        for (int i = 0; i < s1.length(); i++){
             num += (int)s1.charAt(i);
        }
        
        for (int i = 0; i < s2.length(); i++){
             num1 += (int)s2.charAt(i);
        }
        if (num > num1){
            return 2;
            
        }    
        if (num == num1){
            return 0;
            
        }  
        
        if (num < num1){
            return -2;
        }
    }

}

Where do you define the sequence of characters?
Which one is to come first and which one last.
Your code uses the ASCII values. So there will be no difference in ordering for your sort vs any other class's sort.

How can you assign values for the letters so they compare in the order that you want?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.