I'm pretty rusty on my Java skills but I was trying to write a program that prompts the user to enter a string and displays a maximum length increasing ordered subsequence of characters. For example, if the user entered Welcome the program would output Welo. If the user entered WWWWelllcommmeee, the program would still output Welo. I've gotten this much done but it's not doing what it should be and I'm honestly at a loss as to why. My code, if I input Welcome, outputs WWeceeclcccome. If anyone has tips on what I'm doing wrong, they would be much appreciated. Thanks!

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


public class Stuff {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a string. ");
    String userString = input.next();
    ArrayList charList = new ArrayList();
    ArrayList finalList = new ArrayList();
    int currentLength = 0;
    int max = 0;

    for(int i = 0; i < userString.length(); i++){
        charList.add(userString.charAt(i));

        for(int j = i; j < userString.length(); j++){
            int k=j+1;
            if(k < userString.length() && userString.charAt(k) > userString.charAt(j)){
                charList.add(userString.charAt(j));
                currentLength++;

            }

        }


    }

    if(max < currentLength){
        max = currentLength;
        finalList.addAll(charList);
    }



    for (int i = 0; i < finalList.size(); i++){
        char item = (char) finalList.get(i);
        System.out.print(item);

        }

    int size1 = charList.size();
    int size2 = finalList.size();
    System.out.println("");
    System.out.println("Size 1 is: " + size1 + " Size 2 is : " + size2);    
  }

 }

I don't understand how "Welo" is an increasing sequence! I would have thought that "elo" was the answer for "Welcome", and either "el" or "co" from the longer example. Maybe a few more examples would help?

Ah, sorry. It's an increasing subsequece based on ASCII values. A capital W has a lower value than an e, e lower than l, and so on. For example, if I input the word zebras the program should output ers.

Right, of course. I was being a bit slow (not enough cofee yet today!). Sorry.

Did you remember to clear charList before starting to test each new subsequence?

I realized that I didn't do that last night. I updated it a little bit but I'm still not getting to where I need to be. And, honestly I have no idea at this point. Maybe I need more coffee. Hah.

package nkuCSC364;
import java.util.ArrayList;
import java.util.Scanner;


public class Stuff {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a string. ");
        String userString = input.next();
        ArrayList charList = new ArrayList();
        ArrayList finalList = new ArrayList();
        int currentLength = 0;
        int max = 0;

        for(int i = 0; i < userString.length(); i++){            
            for(int j = i; j < userString.length(); j++){
                int k=j+1;
                if(k < userString.length() && userString.charAt(k) > userString.charAt(j)){
                    if(charList.contains(userString.charAt(i))){    
                        charList.add(userString.charAt(j));
                        currentLength++;
                    }
                    else{
                        charList.add(userString.charAt(i));
                        charList.add(userString.charAt(j));
                        currentLength++;
                        currentLength++;
                    }

                }

            }

        }   

            if(max < currentLength){
                max = currentLength;
                finalList.addAll(charList);
            }

            if(max > currentLength){
                charList.clear();
            }






        for (int i = 0; i < finalList.size(); i++){
            char item = (Character) finalList.get(i);
            System.out.print(item);

            }

        int size1 = charList.size();
        int size2 = finalList.size();
        System.out.println("");
        System.out.println("Size 1 is: " + size1 + " Size 2 is : " + size2);    
    }

}

Debugging by re-reading your code and thinking hard is very ineffective. If the error is because you have misunderstood something, or because of something you don't know, you will never find it by reading & thinking. Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong.

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.