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



我已经部分编码了一个程序,该程序应该读取城市列表的文件,确定哪些是最长和最短的名称,并将其输出到一个单独的文件中。然而,我不明白为什么从城市列表中,它没有给我最长和最短的

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.");
}
}

文本文件中的城市列表是亚历山大、弗吉尼亚、奥罗拉、伊利诺伊、奥斯汀、基尼尼克、得克萨斯、波士顿、马萨诸塞、钱德勒、亚利桑那、夏洛特、北卡罗来纳、达拉斯、代顿、俄亥俄、伊丽莎白、新泽西、尤金、俄勒冈州、吉尔伯特、休斯顿、杰克逊、密西西比和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的回应帮助解决了我的字符串读取问题。我仍然想知道我可以把我的栅栏码放在我的长度码的哪里。

因为这段代码:

int len=0, min = 0, max = 0;
.
.
if(len<min) { min=len; shortest=city; }

您需要初始化

最小

大事件。可能是Integer.MAX_VALUE。由于min总是零,所以您的上述条件每次都会失败。

您可以检查以下代码,因此对于问题的第一部分"最短为空白/无">,您需要从文件中获取第一个字符串,并将其长度分配给代码中的maxmin变量,然后从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 palindromen";
}
s="";
return "";
}

输出:

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

相关内容

最新更新