关于使用Java调试时重复输出的问题

关于使用Java调试时重复输出的问题,java,Java,抱歉,如果这是一个答案明显的问题。我正在学习我的第一门面向对象编程课程,这是我第一次使用Java编程。我们应该编写代码来分析包含字符串的文件(由教授提供并链接),以找到列表中最长的回文。我的代码正确地完成了这一点。但是,我选择使用println()方法来查看程序运行时变量“longest”中包含的单词 问题 运行该程序一次导致控制台输出(如下所示)重复7次,我不知道是什么导致了这种情况。任何帮助都将不胜感激 输入文件的前几行 aalii aardvark aardvarks aardwolf a

抱歉,如果这是一个答案明显的问题。我正在学习我的第一门面向对象编程课程,这是我第一次使用Java编程。我们应该编写代码来分析包含字符串的文件(由教授提供并链接),以找到列表中最长的回文。我的代码正确地完成了这一点。但是,我选择使用println()方法来查看程序运行时变量“longest”中包含的单词

问题

运行该程序一次导致控制台输出(如下所示)重复7次,我不知道是什么导致了这种情况。任何帮助都将不胜感激

输入文件的前几行

aalii
aardvark
aardvarks
aardwolf
aba
abaca
abaci
abacist
aback
abacus
abacuses
abaft
abalone
abalones
abamp
abampere
abandon
abandoned
abandonee
abandoner
abandoners
abandoning
abandonment
abandons
作为任务的一部分开发的程序代码

    public class Main {
        
      public String LongestPalindrome(Scanner fileIn ){
        
        String word = "", longest="";
    
    //While loop to obtain strings from sample text file 
        while(fileIn.hasNext())
        {  
          word = fileIn.nextLine();
    
    //Reverse string by characters   
          int wordLength = word.length();
          String reverseWord = "";
    
          for (int i = (wordLength-1); i >= 0; i--)
          {
            reverseWord = reverseWord + word.charAt(i);
          } 
    // Testing to see if string is a palindrome and determining longest palindrome
    
          if (word.equalsIgnoreCase(reverseWord))
          {
            if (longest.length() < word.length())
            {
              longest = word;
              System.out.println(longest + " is now the longest palindrome");
            }
          }
        }
        return longest;
      }

请不要只是重复一遍又一遍的事情来绕过这个网站的功能。相反,在代码之外解释您的问题。我仍然不明白你的问题是什么。@nomadmaker重复输出是我的问题(尽管粘贴时格式不好)它应该是:aba现在是最长的回文abba现在是最长的回文civic现在是最长的回文神化现在是最长的回文malayalam现在是最长的回文malayalam但它重复了7次,我不知道为什么根据我的代码,你可以自由回答你的问题,否则,我怀疑很多人会愿意花时间阅读它。我建议将输出格式化为代码。不用担心java行,只需输入输出即可。另外,请把输入文件的前几行放在这里。谢谢你的建议,这是我第一次在这里提问。我试着按照你的建议修改它。如果我还需要补充什么,请告诉我。
  public static void main(String[] args) {    
    org.junit.runner.JUnitCore.main("Main");
  }


  @Test
  public void test_problem() {
    ArrayList<String> wordCandidate = new ArrayList<String>();
    wordCandidate.add("zymogenic");    
    wordCandidate.add("curators");
    wordCandidate.add("abandonment");
    wordCandidate.add("forewomen");
    wordCandidate.add("malayalam");
    wordCandidate.add("lacings");

    for(String wrd : wordCandidate)
    {
          Scanner fileIn = null;  // Initializes fileIn to an empty object
          try
          {
            // Attempt to open the file
            fileIn = new Scanner(new FileInputStream("words.txt"));
            if (!wrd.contains("mal"))
              assertNotSame(wrd, LongestPalindrome(fileIn));
            else
              assertEquals(wrd,LongestPalindrome(fileIn));
          }
          catch (FileNotFoundException e)
          {
            // If the file could not be found, this code is executed
            // and then the program exits
            System.out.println("File not found.");
            System.exit(0);
          }
aba is now the longest palindrome
abba is now the longest palindrome
civic is now the longest palindrome
deified is now the longest palindrome
malayalam is now the longest palindrome
aba is now the longest palindrome
abba is now the longest palindrome
civic is now the longest palindrome
deified is now the longest palindrome
malayalam is now the longest palindrome
aba is now the longest palindrome
abba is now the longest palindrome
civic is now the longest palindrome
deified is now the longest palindrome
malayalam is now the longest palindrome
aba is now the longest palindrome
abba is now the longest palindrome
civic is now the longest palindrome
deified is now the longest palindrome
malayalam is now the longest palindrome
aba is now the longest palindrome
abba is now the longest palindrome
civic is now the longest palindrome
deified is now the longest palindrome
malayalam is now the longest palindrome
aba is now the longest palindrome
abba is now the longest palindrome
civic is now the longest palindrome
deified is now the longest palindrome
malayalam is now the longest palindrome

Time: 1.745

OK (1 test)