0

So, I have an ArryList, and there is a toString method used to print the Strings contained in the ArrayList.

However, whenever I run it, I either get nothing in response or some garbled computer language in return.

What am I missing, or doing wrong?

import java.util.Scanner;
public class main
{
    public static void main (String [] args)
{
    while (1 == 1)
    {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter a name.");
        System.out.println();
        System.out.print("-->  ");
        String namae = scan.nextLine();
        String checkstop = namae;
        checkstop = checkstop.toLowerCase();
        String n1 = namae.substring(0, 1);
        n1 = n1.toUpperCase();
        String n2 = namae.substring(1, (namae.length()));
        namae = n1+n2;

        for (int i = 0; i < namae.length(); i++)
        {
            char checker = namae.charAt(i);
            String checks = checker + "";
            if (checks.equals(" "))
            {
                char change = namae.charAt(i + 1);
                String changes = change + "";
                changes = changes.toUpperCase();
                String z1 = namae.substring(0, (i));
                String z2 = change + (namae.substring((i + 2), (namae.length())));
                namae = z1 + z2;
            }
        }

        if (namae.length() < 3)
        {
            System.out.println("Invalid Input! Too few characters.");
        }
        else if (checkstop.equals("stop"))
        {
            break;
        }
        else
        {
            send(namae);
        }
    }
}

public static void send(String namae)
{
    InsertionSort s = new InsertionSort(namae);
    }
}

and the constructor class:

import java.util.ArrayList;
public class InsertionSort
{
String namae;

public InsertionSort(String namae)
{               
    this.namae = namae;
}

public void Sort()
{
    ArrayList<String> list = new ArrayList<String>();
    list.add(namae);

    for (int i = 1; i < list.size(); i++)
    {
        int j = i;
        String tmp = list.get(i).substring(0, 1);
        for(j = i - 1; j >= 0; j--)
        {
            if ((tmp.compareTo(list.get(j).substring(0, 1))) > 0)
            {
                list.set(j ,tmp);
            }
            if ((tmp.compareTo(list.get(j).substring(0, 1))) == 0)
            {
                list.set(j ,tmp);
            }
            if ((tmp.compareTo(list.get(j).substring(0, 1))) < 0)
            {
                list.set(j ,tmp);
            }
        }
    }

    for (int g = 0; g < list.size(); g++)
    {
        toString(list.get(g));
    }
}

public static String toString(String x)
{
    System.out.println(x);
    return x;
}
}
arsb48
  • 563
  • 4
  • 10

3 Answers3

1

As you Sort method is never being called, change you code to be

public static void send(String namae)
{
    InsertionSort s = new InsertionSort(namae);
    s.Sort ();
}

Also please stick to java coding standards i.e. methods should start with a lowercase

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

Also, you're never calling the Sort() method in your program.

InsertionSort s = new InsertionSort(namae);
s.Sort(); // Perform sorting.
ck1
  • 5,243
  • 1
  • 21
  • 25
  • ha - beat you to it ;-) – Scary Wombat Jun 17 '16 at 01:12
  • Nice job @ScaryWombat! – ck1 Jun 17 '16 at 01:14
  • I tried doing what you suggested, but now all it does is print the String that is being added to the ArrayList. Is there a way to print the contents of the ArrayList? – arsb48 Jun 17 '16 at 01:17
  • It *is* printing the contents of the `ArrayList`. It looks like there's a bug in your `Sort()` algorithm? What are you expecting to be outputted? – ck1 Jun 17 '16 at 01:20
  • Several lines, with the names in alphabetical order. That part might not work, but it should print all of the names that are entered. – arsb48 Jun 17 '16 at 01:29
  • @arsb48 Maybe because the three `if` statements inside the two loops cover all cases and does the *same* thing: `if a>0 doX; if a==0 doX; if a<0 doX` All values of `a` will cause `doX` to be called. `if` statements are meaningless. – Andreas Jun 17 '16 at 01:50
  • @Andreas Not only this. In his `Sort` method which is not even being called, he instantiates a new `ArrayList` each time and only adds one name to it. – Scary Wombat Jun 17 '16 at 02:25
0

Just iterate over the ArrayList and print each element:

for (String str : list) {
    System.out.println(str);
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360