按字母顺序排列字符串和反向字符串?

时间:2014-12-08 00:04:05

标签: java string sorting

对于这个程序,我需要有用户输入字符串,然后将它们与反向对应字符串放在一个数组中。然后整个数组将按字母顺序排列。所以它应该像这样工作:

  • 输入:草莓香蕉,苹果,葡萄

  • 输出:苹果,ananab,香蕉,elppa,葡萄,separg,草莓,yrrebwarts

我有这个代码,它在某种程度上有效。但是,它只是将反转和正常按字母顺序排列,而是分开进行。所以最终看起来像这样:

  • 产量:ananab,elppa,separg,yrrebwarts,苹果,香蕉,葡萄,草莓

正如你所看到的那样 在某种程度上按字母顺序排列单词,但不是它应该如何。这是我的代码:

import java.util.Scanner;

public class WordGame {

  public static void main(String[] args) { 
    String reverseInput = " "; //creates an empty string where the reverse will be stored before       putting it into the array
    String[] wordArray = new String[1000]; //creates an array that allows for 500 words, and 500     reverses of said words
    int s = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter the words you would like to reverse. To finish entering words enter a blank line:");
    String userInput = sc.nextLine(); //allows for user to input words

    int length = userInput.length();
    int i = 0;

    while (!userInput.equals("")){ //runs until the user enters a blank line
      reverseInput = " ";
      for(i = length - 1; i >= 0; i--)
        reverseInput += userInput.charAt(i); 
      wordArray[s] = userInput; //reverses user inputted strings by taking the last letter and putting it in front, repeating until the whole word is reversed
      wordArray[s + 1] = reverseInput; 
      s += 2;

      userInput = sc.nextLine();
      length = userInput.length();
    }

    for(int j = 0; j < s-1; j++){ //beginning of alphabetical sorting
      for(int k = 0; k < s-1-j; k++){
        int l = 0;
        while((int)wordArray[k].charAt(l) == (int)wordArray[k+1].charAt(l))
          l++;
        if ((int)wordArray[k].charAt(l) > (int)wordArray[k+1].charAt(l)){
          String holder = wordArray[k];
          wordArray[k] = wordArray[k+1];
          wordArray[k+1] = holder;
        }
      }

    }

        for(i = 0; i < wordArray.length; i++){
          if (wordArray[i]!= null){
            System.out.print(wordArray[i] + " "); //prints out contents of array
        }
        }
      }
    }

我不确定问题是什么。任何帮助将非常感激。谢谢!

2 个答案:

答案 0 :(得分:1)

据我所知,你在“reverseInput”前面加了一个额外的空白reverseInput = " "; 因为您的反向字符串不会以您认为的char开头(它以空白开头),结果不是您真正想要的。尝试删除空白并重试您的代码。

答案 1 :(得分:0)

肯定有更简单的方法可以做到这一点,具体取决于你是否想要使用Java的内置类来完成这类工作。我刚写完了,它完成了你想要的反向字符串的排序。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution {
    public static void main(String args[]) {
        ArrayList<String> values = new ArrayList<String>();
        values.add("strawberry");
        values.add("banana");
        values.add("apple");
        values.add("grapes");
        Solution s = new Solution();
        values = s.sortList(values);
        int itemCt = 1;
        for (String item : values) {
            System.out.println(itemCt + ": " + item);
            itemCt++;
        }
    }

    public ArrayList<String> sortList(List<String> strings) {
        ArrayList<String> combinedList = new ArrayList<String>();
        for (String str : strings) {
            combinedList.add(str);
            combinedList.add(new StringBuilder(str).reverse().toString());
        }
        Collections.sort(combinedList);
        return combinedList;
    }

}