Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要这个程序在Java中查找字符串中最长的单词_Java_Java.util.scanner - Fatal编程技术网

我需要这个程序在Java中查找字符串中最长的单词

我需要这个程序在Java中查找字符串中最长的单词,java,java.util.scanner,Java,Java.util.scanner,代码不起作用。提示用户是成功的,但没有其他事情发生。如果您输入一个字符串并按Enter键,它将转到下一行,在那里您可以再次输入,等等。循环中应该有一个退出条件。请使用以下代码 import java.util.*; class Exam3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a String: "

代码不起作用。提示用户是成功的,但没有其他事情发生。如果您输入一个字符串并按Enter键,它将转到下一行,在那里您可以再次输入,等等。

循环中应该有一个退出条件。请使用以下代码

import java.util.*;
class Exam3
{
      public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a String: ");

    String word1 = "", word2 = "";
    int l1 = 0, l2 = 0;

    while(sc.hasNext())
    {   word1 = sc.next();
        l1 = word1.length();
        if(l1 > l2)
        {
            l2 = l1;
            word2 = word1;
        }
    }

    System.out.println("Longest Word: " + word2);
    System.out.println("Length of Word: " + l2);
}
}

这种方法对我有效

import java.util.Scanner;

public class LongestString {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a String: ");

        String word1 = "", word2 = "";
        int l1 = 0, l2 = 0;

        while (sc.hasNext()) {
            word1 = sc.next();

            // type exit finish the loop
            if (word1.equals("exit"))
                break;

            l1 = word1.length();
            if (l1 > l2) {
                l2 = l1;
                word2 = word1;
            }
        }

        sc.close();

        System.out.println("Longest Word: " + word2);
        System.out.println("Length of Word: " + l2);
    }
}

你试过调试它吗?你得到了什么结果?适用于我可能重复:我试着在在线编译器上编译它,结果成功了!我不知道它在BlueJP中是如何工作的。请看上面的链接。基本上,
hasNext()。相反,您可能希望使用诸如“exit”之类的字符串作为退出循环的提示。
public class actual {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a String: ");

        int largestLength = 0;
        String largestWord = "";
        String a = sc.nextLine();
        for(String b:a.split(" ")){
            if (largestWord.length() == 0) {
                largestLength = b.length();
                largestWord = b;
            } else if (b.length() >= largestLength) {
                largestLength = b.length();
                largestWord = b;
            }
        }
        sc.close();
        System.out.println("Longest Word: " + largestWord);
        System.out.println("Length of Word: " + largestLength);
    }
}