我只需要将8位数字而不是单词的数字存储到数组中,如果不是,则只需将其打印到控制台。一旦进入数组,我必须对它们进行排序并将它们打印到右侧,而左侧则是未排序的列表。
所以我被困在一个带有逗号的文件中,只有当它没有逗号或空格时才有效。我应该使用"compareTo"one_answers"StringTokenizer"方法,我知道它们是如何工作的,但只是不做我想做的,也许我把它放在错误的函数中。我还需要把这个文件分开,放在一个单独的文件中,GUI函数不确定在那个文件上放什么。
public class Project1 {
static final int LIST_SIZE = 10;
static int ssnSize;
static String line;
static String[] ssnList;
static TextFileInput inFile;
static String inFileName = "Dates.txt"; //save the file in Lab12 folder on BB in your project folder
static JFrame myFrame;
static Container myContentPane;
static TextArea left, right;
public static void main(String[] args) {
initialize();
readNumbersFromFile(inFileName);
printSSNList(ssnList,ssnSize);
printSSNtoJFrame(myFrame,ssnSize);
}
public static void initialize() {
inFile = new TextFileInput(inFileName);
ssnList= new String[LIST_SIZE];
ssnSize=0;
line ="";
left = new TextArea();
right = new TextArea();
myFrame = new JFrame();
myFrame.setSize(400, 400);
myFrame.setLocation(200, 200);
myFrame.setTitle("");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void readNumbersFromFile(String fileName)
{
String ssn;
ssn = inFile.readLine();
while (ssn != null) {
assert (isValidDate(ssn)): "SSN not valid";
if (!isValidDate(ssn))
throw new IllegalArgumentException("Invalid SSN");
else
storeDates(ssn,ssnList);
ssn = inFile.readLine();
} //while
} //readSSNsFromFile
public static void printSSNList(String[] list, int size)
{
assert (isValidList(list)): "The array is not valid";
if (!isValidList(list)){
throw new IllegalArgumentException("Invalid list)");
}
for (int i=0;i<size;i++)
if (!isValidDate(list[i]))
System.out.println("Invalid SSN: "+list[i]);
else
System.out.println(list[i]);
}
public static void storeDates(String s, String[] list)
{
assert (isValidDate(s)): "The SSN is not valid";
assert (isValidList(list)): "The array is not valid";
if (isValidDate(s) && isValidList(list))
list[ssnSize++]=s;
assert (isValidList(list)):"Resulting list not valid";
}
public static void printSSNtoJFrame(JFrame jf, int size)
{
assert (isValidList(ssnList)): "The array is not valid";
if (!isValidList(ssnList)){
throw new IllegalArgumentException("Invalid list)");
}
jf.setLayout(new GridLayout(1, 2));
myContentPane = jf.getContentPane();
TextArea myLeftArea = new TextArea();
TextArea myRightTextArea = new TextArea();
myContentPane.add(myLeftArea);
myContentPane.add(myRightTextArea);
for (int i=0;i<size;i++)
{
if (!isValidDate(ssnList[i]))
myLeftArea.append("Invalid SSN: "+ssnList[i]+"n");
else
{
myLeftArea.append(ssnList[i]+"n");
}
}
sortOnlyNumbers(ssnList);
for(int j=0; j< size; j++)
{
myRightTextArea.append(ssnList[j]+"n");
}
jf.setVisible(true);
}
private static void sortOnlyNumbers(String[] array)
{
List<Integer> indexes = new ArrayList<Integer>();
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
try {
numbers.add(Integer.parseInt(array[i]));
indexes.add(i);
} catch (NumberFormatException e) {
// don't care
}
}
Collections.sort(numbers, Collections.reverseOrder());
for (int i = 0; i < numbers.size(); i++) {
array[indexes.get(i)] = String.valueOf(numbers.get(i));
}
}
public static boolean isValidDate(String s)
{
if (s.length() != 8) {
throw new IllegalArgumentException("An SSN length must be 9");
}
for (int i=0;i<8;i++)
if (! Character.isDigit(s.charAt(i))) {
throw new IllegalArgumentException("SSN must have only digits.");
}
return (true);
}
public static boolean isValidList(String[] list)
{
if (list == null){
return false;
}
if (ssnSize == list.length){
return false;
}
return (true);
}
}
文本文件有以下内容:
2016100120080912, 20131120, 19980927, n20020202,你好20120104
您可以使用正则表达式来执行此操作。适合您需求的格式是:
(d{8})
这个正则表达式匹配输入数据中8个连续数字的组。
我使用下面的代码片段进行了测试,并且能够从输入字符串中检索到所有8位数字。public class Snippet {
public static void main(String[] args) {
String input = "20161001 20080912,20131120,19980927, n 20020202,hello 20120104";
List<String> matches = get8DigitNumbersOnly(input);
System.out.println(matches);
}
public static List<String> get8DigitNumbersOnly(String inputData) {
Pattern pattern = Pattern.compile("(\d{8})"); // This is the regex.
Matcher matcher = pattern.matcher(inputData);
List<String> matches = new ArrayList<String>();
while(matcher.find()) {
String match = matcher.group();
matches.add(match);
}
return matches;
}
}
给它注射。希望这对你有帮助!