Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating though a text file in Java so that each line has information about that line
    primarykey
    data
    text
    <p>Ok, so I really didn't know how to say it right for the title so this should shed some light on the situation.</p> <p>I'm making a palindrome program in Java. In every which way you look at it, it works just fine. It reads in a file using <code>Scanner</code>, searches through the entire file and outputs if that line in the text file is a palindrome. If you have any special characters or caps it deletes them and turns everything to lowercase.</p> <p>My issue is that after the check is done on each line, I want to show some extra information next to the result.</p> <p>Each line should show how many words are in the line, how many characters and if its a palindrome or not.</p> <p>Anyway here is the code, hopefully someone can help me figure this out. Thanks.</p> <pre><code>import java.io.File; import java.util.Scanner; public class Palindrome { public static void main(String[] args) { //Global Variables Scanner cScan = null; Scanner wScan = null; Scanner pScan = null; int charCount = 0, numLines = 0, numChars = 0, wordCount = 0; //Take in User Input Scanner iScan = new Scanner(System.in); //Start input Scanner String fileName = null; System.out.print("Please Enter a File Name: "); fileName = iScan.nextLine(); iScan.close(); //Close input Scanner //Read File Specified by User File palin = new File(fileName); try { //Checks for Number of Characters cScan = new Scanner(palin); while(cScan.hasNextLine()) { String line = cScan.nextLine(); numChars += line.length(); numLines++; } //Checks for Number of Words wScan = new Scanner(palin); while (wScan.hasNext()) { wScan.next(); wordCount++; } //Format Lines pScan = new Scanner(palin); while (pScan.hasNext()) { String line = pScan.nextLine(); String reString = line.replaceAll("[^\\p{L}\\p{Nd}]", ""); String lString = reString.toLowerCase(); boolean pali = false; String tP = "Yes", fP = "No"; int n = lString.length(); for (int i = 0; i &lt; (n / 2) + 1; ++i) { if (lString.charAt(i) != lString.charAt(n - i - 1)) { pali = false; break; } else if (lString.charAt(i) == lString.charAt(n - i - 1)) { pali = true; break; } } if (pali == true) System.out.println(line + " w: " + wordCount + ", " + " c: " + charCount + ", " + tP); else System.out.println(line + " w: " + wordCount + ", " + " c: " + charCount + ", " + fP); } } catch(Exception e) { System.out.println("File Could Not be Found"); } //charCount = (numLines + numChars) - 1; //Minus 1 to Compensate for EOL at EOF //System.out.println(charCount); //System.out.println(wordCount); //System.out.println(spRemover); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload