Hi everyone,

First off - I'm very new to Java so please be gentle. I'm having some problems with an assignment from school:

The input starts with an integer k indicating the number of words in the text (max 1000), followed by k words.


The output will consist of a single string containing all characters that appear in the text, printed in alphabetical order, followed by all the numbers that appear in the text, printed in order. Each character will be printed only once (even if there is more than an 'a' in the text, the 'a' appear only once in the output). Do not distinguish between uppercase and lowercase.. Ignore all characters not letters a-z (or A-Z) or numbers 0-9.

Example input:
Donald Duck was first published in 1948.

The corresponding output:

I'm thinking the following: add every character to an ArrayList and then sort it and output everything as a string. My problem so far is that my program seems to get stuck after the first word (which it adds to my ArrayList) but then gets stuck at "object exists"...Any ideas why? Am I approaching this correctly?

Here is my code:

import java.util.Scanner;
import java.util.ArrayList;

public class Sort {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number followed by the same ammount of words: ");
        String word;
        int numberOfWords;
        numberOfWords = input.nextInt();
        word = input.next();
        int wordLen = word.length();
        ArrayList<Character> characterArray = new ArrayList<Character>();
        for (int v = 0; v < numberOfWords; v++) {
        	for (int i = 0; i < wordLen; i++) {
        		if(characterArray.contains(word.charAt(i))) {
        			System.out.println("Object exists");
        		}	else {
/*        String outString =  new String(characterArray);

You only read in 1 word from the user (line 11). That needs to be in some kind of loop somewhere to read in the right number of words - either read them all into an array then process them, or read them one at a time and process each one straight away.

This article has been dead for over six months. Start a new discussion instead.