Java-函数似乎弄乱了控制台,调用该函数后不会打印任何内容

标签 java eclipse java.util.scanner

我被要求编写一个方法来接收扫描仪,并返回一个排序的单词数组,其中仅包含字母,没有重复(长度不超过 3000)。然后,我被要求编写一个方法来检查给定的词汇表中是否包含某个给定的字符串。我使用了简单的二分查找方法。 这就是我所做的:

public static String[] scanVocabulary(Scanner scanner){       
		String[] array= new String[3000];
		int i=0;
		String word;
		while (scanner.hasNext() && i<3000) {
			word=scanner.next();
			if (word.matches("[a-zA-Z]+")){
				array[i]=word.toLowerCase();
				i++;
			}
		}int size=0;
		while (size<3000 && array[size]!=null ) {
			size++;
		}
		String[] words=Arrays.copyOf(array, size);
		if (words.length==0 || words.length==1) {
			return words;
		}
		else {
			Arrays.sort(words);
			int end= removeDuplicatesSortedArr(words);
			return Arrays.copyOf(words, end);
		}

	}
	
	private static int removeDuplicatesSortedArr(String[] array) { //must be a sorted array. returns size of the new array
		int n= array.length;
		int j=0;
		for (int i=0; i<n-1; i++) {
			if (!array[i].equals(array[i+1])) {
				array[j++]=array[i];
			}
		}
		array[j++]=array[n-1];
		return j;
	}
	
	public static boolean isInVocabulary(String[] vocabulary, String word){ 
		//binary search
		int n=vocabulary.length;
		int left= 0;
		int right=n-1;
		while (left<=right) {
			int mid=(left+right)/2;
			if (vocabulary[mid].equals(word)){
				return true;
			}
			else if (vocabulary[mid].compareTo(word)>0) {
				right=mid-1;
			}else {
				right=mid+1;
			}
		}
		return false;
	}

尝试以下代码时:

public static void main(String[] args) {

        String vocabularyText = "I look at the floor and I see it needs sweeping while my guitar gently weeps";
        Scanner vocabularyScanner = new Scanner(vocabularyText);
        String[] vocabulary = scanVocabulary(vocabularyScanner);
        System.out.println(Arrays.toString(vocabulary));

        boolean t=isInVocabulary(vocabulary, "while");
        System.out.println(t);
        System.out.println("123");

    }

我什么也没得到,除了-

[and, at, floor, gently, guitar, i, it, look, my, needs, see, sweeping, the, weeps, while]

没有其他内容被打印出来或返回。这两个功能似乎单独工作得很好,所以我不明白我做错了什么。 我很高兴听到您的想法,提前致谢:)

最佳答案

这与控制台无关。您的 isInVocabulary 方法在此 block 中进入无限循环:

    if (!isInVocabulary(vocabulary, "while")) {
        System.out.println("Error");
    }

如果您通过 isInVocabulary 进行调试,您会看到在 while 循环迭代几次后,

left = 0;
right = 2;

mid = 1;

if (vocabulary[mid].equals(word)){
    // it doesn't
} else if (vocabulary[mid].compareTo("while") > 0) {
    // it doesn't
} else {
    right = mid + 1;
    // this is the same as saying right = 1 + 1, i.e. 2
}

所以你会永远循环。

关于Java-函数似乎弄乱了控制台,调用该函数后不会打印任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430696/

相关文章:

java - 程序需要一直循环直到键入键 "Q"/"q"

Java 线程与 Scanner 第一次工作但第二次就不行了?

java - Hbox 有子 Buttons ,如何抓取它们并为其添加监听器?

java - 在项目上找不到参数[src/test/resources/testSuite.xml]的方法suites()

java - 在 Spring 的 WebServiceTemplate 中设置超时

java - Eclipse 无法识别 com.sun.net.httpserver.HttpServer 包

java - 使用新参数在循环中创建新线程

c++ - 在 Eclipse 上启用 C++17 以使用 `std::byte`

java - 从其他 Java 类调用方法

Java:使用 Scanner 在方法中添加项目