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;
}
}
3
  • As you only have one String, I am not sure why you are putting it into a List Commented Jun 17, 2016 at 1:06
  • BTW, what is your input and what is your output? Commented Jun 17, 2016 at 1:07
  • @sfjac I think the best/easiest way to convert a List to a string, is calling the toString() method. Maybe I'm just too lazy. Commented Jun 17, 2016 at 1:52

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, sorry. I usually convert to camel-case after I finish the code.
Now only CamelCase, but lowerCamelCase as well.
1

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

InsertionSort s = new InsertionSort(namae);
s.Sort(); // Perform sorting.

7 Comments

ha - beat you to it ;-)
Nice job @ScaryWombat!
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?
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?
Several lines, with the names in alphabetical order. That part might not work, but it should print all of the names that are entered.
|
0

Just iterate over the ArrayList and print each element:

for (String str : list) {
    System.out.println(str);
}

1 Comment

does not explain why his code does not work though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.