当我输入用户输入时,为什么这个程序会终止?

时间:2016-01-09 20:39:46

标签: java eclipse string macos

该程序应该将用户输入的字符串与文本文档进行比较。如果程序在文件和部分字符串中找到匹配项,则应突出显示或更改用户输入的匹配字符串的字体颜色。问题是,一旦我输入用于用户输入的内容,程序就会终止。可以在文件中匹配的输入示例是MALEKRQ,MALE,MMALEKR,MMMM和MALEK。我该如何解决这个问题?我在Mac OS X El Capitan上使用Eclipse Neon。

import java.util.*;
import java.io.*;

public class ScienceFair 
{

public static void main(String[] args) throws FileNotFoundException 
{
    java.io.File file = new java.io.File("/Users/Kids/Desktop/ScienceFair/src/MALEKRQsample.txt");
    try 
    {
        Scanner fileInput = new Scanner(file);
        Scanner userInput = new Scanner(System.in);
        System.out.println("Enter Protein Sequence");
        String userProteinSequence = userInput.nextLine().toUpperCase();

        int len = userProteinSequence.length();
        int size = 4;
        int start = 0;
        int indexEnd = size;
        while (indexEnd < len - size) 
        {
            for (int index = start; index <= len - size; index++) 
            {
                String search = userProteinSequence.substring(index, indexEnd);
                System.out.println(search);
                while (fileInput.hasNext()) 
                {
                    String MALEKRQ = fileInput.nextLine();
                    // System.out.println(MALEKRQ);
                    int found = MALEKRQ.indexOf(search);
                    if (found >= 0) 
                    {
                        System.out.println("Yay.");
                    } 
                    else 
                    {
                        System.out.println("Fail.");
                    }
                }
                indexEnd++;

            }
            size++;
            if (size > 8) {
                size = 8;
                start++;
            }   
        }
    } 

    catch (FileNotFoundException e) 
    {
        System.err.format("File does not exist.\n");
    }
}
}

1 个答案:

答案 0 :(得分:2)

import java.util.*;
import java.io.*;

public class ScienceFair 
{

public static void main(String[] args) throws FileNotFoundException 
{
    java.io.File file = new java.io.File("/Users/Kids/Desktop/ScienceFair/src/MALEKRQsample.txt");
    try 
    {
        Scanner userInput = new Scanner(System.in);
        System.out.println("Enter Protein Sequence");
        String userProteinSequence = userInput.nextLine().toUpperCase();

        for (int size = userProteinSequence.length(); size >= 4; size--) {
            for (int start = 0; start <= userProteinSequence.length()-size; start++) {
                boolean found = false;
                String search = userProteinSequence.substring(start, size);
                System.out.println(search);

                Scanner fileInput = new Scanner(file);
                while (fileInput.hasNext()) {
                    String MALEKRQ = fileInput.nextLine();
                    int found = MALEKRQ.indexOf(search);
                    if (found >= 0) {
                        found = true;
                    }
                }
                if (found) {
                    System.out.println(search+" found (index "+start+")");
                    fileInput = new Scanner(file);
                    while (fileInput.hasNext()) {
                        String MALEKRQ = fileInput.nextLine();
                        MALEKRQ = MALEKRQ.replaceAll(search, "[["+search+"]]");
                        System.out.println(MALEKRQ);
                    }
                    return;
                }
            }
        } 

    System.out.println(search+" not found");
    Scanner fileInput = new Scanner(file);
    while (fileInput.hasNext()) {
        String MALEKRQ = fileInput.nextLine();
        System.out.println(MALEKRQ);
    }

    } catch (FileNotFoundException e) {
        System.err.format("File does not exist.\n");
    }
}
}