开发者社区> 问答> 正文

输出文件中最长和最短的文本,并发现回文不起作用?

我已经对程序进行了部分编码,该程序应该读取城市列表的文件,确定最长和最短的名称,然后将其输出到单独的文件中。但是,我无法从城市列表中找出原因,它没有给我最长和最短的时间

import java.io.*;
import java.util.Scanner;
public class testing
{
    public static void main(String args[])throws IOException
    {
        Scanner sc = new Scanner(new File("C:/code/names.txt"));
        String city="", longest="", shortest="";
        int len=0, min = 0, max = 0;

        while(sc.hasNext())
        {
            city= sc.nextLine();
            len= city.length();
            if(len>max)
            {
                max=len;
                longest=city;
            }
            if(len<min)
            {
                min=len;
                shortest=city;
            }
        }
        System.out.println(longest + " is the longest name.");
        System.out.println(shortest + " is the shortest name.");
    }
}

文本文件中的城市列表为亚历山大,弗吉尼亚,奥罗拉,伊利诺伊州,奥斯丁,基尼基尼克,德克萨斯州,波士顿,马萨诸塞州,钱德勒,亚利桑那州,夏洛特,北卡罗来纳州,达拉斯,代顿,俄亥俄州,伊丽莎白,新泽西州,尤金,俄勒冈州,吉尔伯特(Gilbert),休斯顿(休斯顿),杰克逊(Jackson),密西西比(Mississippi)和格莱内尔格(Glenelg),我总是得到的输出最长的是马萨诸塞州,最短的是空白/什么都不是,这是不正确的。我也想知道如何/在哪里将这段代码合并到我的代码中

import java.io.*;
import java.util.Scanner;
public class palindrome_demo
{
public static void main(String args[]) throws IOException
 {

    String s="";
    Scanner sc= new Scanner(new File("c:/code/names.txt"));
    while(sc.hasNextLine())
        {
            String word = sc.nextLine();

            for(int i = word.length() - 1; i >= 0; i--)
            {
                s = s + word.charAt(i);
            }

            if (word.equalsIgnoreCase(s))
            {
                System.out.println(word + " is a palindrome");
            }
            s="";
            }
        sc.close();
   }
}

它可以完美地在文件中找到回文,但是我不确定在没有无限循环的情况下将其放置在何处。

TLDR;从文件读取时,我的代码没有给我正确的最长和最短的单词,我想知道在哪里可以将单独的代码放在我的第一个代码中,以便它们可以一起运行。最后,我还想将所有这些信息(最长,最短和回文词)输出到单独的txt中,但是我不确定如何输出。

*编辑:anupamD的回复帮助解决了我的字符串阅读问题。我仍想知道在哪里可以将我的回文代码放在长度代码中。

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 14:11:21 766 0
1 条回答
写回答
取消 提交回答
  • 您可以查看下面的代码,所以对你的问题的第一部分“空白/没有为最短”你需要从文件中获得的第一个字符串和它的长度分配到两个max和min变量在你的代码,并随后得到了下一个字符串while循环中的文件,无论如何你已经在做。在代码中的问题是你min一直都是,0而你if(len<min)永远都不是真实的,这就是为什么每次都会得到最短的字符串为空的原因。

    对于问题的第二部分,*如何/在何处将这段代码合并到我的代码(即回文代码)中,您可以创建一个单独的方法,findPalindrome()并在找到最短代码后从现有代码中调用此方法。和文件中最长的字符串。

    public class Practice {
    
        public static void main(String args[])throws IOException {
            Scanner sc = new Scanner(new File("c:/code/names.txt"));
            String city="", longest="", shortest="";
            int len=0, min = 0, max = 0;
    
            if(sc.hasNext()) {
                 city= sc.nextLine();
                 len= city.length();
                 min = len;
                 max=len;
                 longest=city;
                 shortest=city;
            }
    
            while(sc.hasNext()) {
                city= sc.nextLine();
                len= city.length();
                if(len>max)
                {
                    max=len;
                    longest=city;
                }
                if(len<min)
                {
                    min=len;
                    shortest=city;
                }
            }
            System.out.println(longest + " is the longest name.");
            System.out.println(shortest + " is the shortest name.");
            findPalindrome();
        }
    
    
        public static void findPalindrome() throws FileNotFoundException {
            String s="";
            Scanner sc= new Scanner(new File("c:/code/names.txt"));
            while(sc.hasNextLine()) {
                String word = sc.nextLine();
    
                for(int i = word.length() - 1; i >= 0; i--){
                    s = s + word.charAt(i);
                }
    
                if (word.equalsIgnoreCase(s)){
                    System.out.println(word + " is a palindrome");
                }
                s="";
            }
            sc.close();
        }
    }
    

    输出:

    Massachusetts is the longest name.
    Ohio is the shortest name.
    Kinikinik is a palindrome
    Glenelg is a palindrome
    

    如果您不想两次读取文件,则可以使用以下代码

    public static void main(String args[])throws IOException {
        Scanner sc = new Scanner(new File("c:/code/names.txt"));
        String city="", longest="", shortest="";
        int len=0, min = 0, max = 0;
        String palindromeStr = "";
        if(sc.hasNext()) {
             city= sc.nextLine();
             len= city.length();
             min = len;
             max=len;
             longest=city;
             shortest=city;
             palindromeStr = findPalindrome(city);
        }
    
        while(sc.hasNext()) {
            city= sc.nextLine();
            len= city.length();
            if(len>max)
            {
                max=len;
                longest=city;
            }
            if(len<min)
            {
                min=len;
                shortest=city;
            }
            palindromeStr += findPalindrome(city);
        }
        System.out.println(longest + " is the longest name.");
        System.out.println(shortest + " is the shortest name.");
        System.out.println(palindromeStr);
    }
    
    
    public static String findPalindrome(String word) throws FileNotFoundException {
        String s="";
        for(int i = word.length() - 1; i >= 0; i--){
            s = s + word.charAt(i);
        }
        if (word.equalsIgnoreCase(s)){
           return word + " is a palindrome\n";
        }
        s="";
        return "";
    }
    

    输出:

    Massachusetts is the longest name.
    Ohio is the shortest name.
    Kinikinik is a palindrome
    Glenelg is a palindrome
    

    回答来源:Stack Overflow

    2020-03-26 14:12:09
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载