Outputting longest and shortest text in a file, and finding palindrome not working?

Viewed 92

I've partially coded a program that's supposed to read a file of a list of cities, determine which are the longest and shortest name-wise, and output it into a separate file. However, I can't figure out why from the list of cities, it's not giving me the longest and shortest

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

The list of cities in the text file are Alexandria, Virginia, Aurora , Illinois, Austin, Kinikinik, Texas, Boston, Massachusetts, Chandler, Arizona, Charlotte, North Carolina, Dallas, Dayton, Ohio, Elizabeth, New Jersey, Eugene, Oregon, Gilbert, Houston, Jackson, Mississippi, and Glenelg, and the output I always get is Massachusetts for longest and a blank/nothing for shortest, which is incorrect. I was also wondering how/where can I incorporate this piece of code into my code

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();
   }
}

It finds the palindrome in the file perfectly, but I'm not sure where to place it without it looping infinitely.

TLDR; My code isn't giving me the correct longest and shortest word when reading from a file, and I'd like to know where can I place my separate code into my first code, so that they can both run together. In the end, I'd also like to output all of this info(longest,shortest, and palindrome words) to a separate txt, but I'm not sure how.

*EDIT: anupamD's response helped solve my string reading problem. I'd still like to know where can I place my palidrome code in my length code.

2 Answers

Because of this piece of code:

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

You need to initialise the

min

to something big. May be Integer.MAX_VALUE. Since min is always zero, your above condition will fail every time.

You can check the following code, so for the first part of your question "blank/nothing for shortest" you need to get the first string from your file and assign its length to both max and min variables in your code and subsequently get the next string from file in your while loop, that you already doing anyway.In your code problem was your min was always 0 and your if(len<min) was never be true that is why you were getting shortest string as empty every time.

And for second part of your question *how/where can I incorporate this piece of code into my code( i.e. palindrome code) *, you can create a separate method findPalindrome() and call this method from your existing code, once when you done with finding shortest and longest string from your file.

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();
    }
}

Output:

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

And if you do not want to read your file twice, you can use the below code

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

Output:

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