Hello, I am stuck with a program that I am currently doing right now. I am new t
ID: 3542304 • Letter: H
Question
Hello, I am stuck with a program that I am currently doing right now. I am new to Java and I am still learning. The code to my program is as folows:
public class Aa {
/* Counts number of characters in each line
(newline counts as a character) */
public static int countChars(String str4) {
return str4.length() + 1;
}
/* Counts number of words */
public static int words(String str2) {
int wordCount = 0;
String[] words2 = str2.split(" ");
wordCount += words2.length;
return wordCount;
}
/* Checks if the input line is a palindrome */
public static boolean isPalindrome(String str3) {
int left = 0;
int right = str3.length()-1;
while (left < right)
{
if (str3.charAt(left) != str3.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
BufferedReader br = null;
/* Reads line by line and does stuff to it */
try {
String currentLine;
br = new BufferedReader(new FileReader("p1-test4.in"));
while ((currentLine = br.readLine()) != null) {
String lettersOnly = currentLine.replaceAll("[\W]", "");
String lowerCase = lettersOnly.toLowerCase();
System.out.println("w: " + words(currentLine) + " c: " + countChars(currentLine) +
", " + isPalindrome(lowerCase));
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
The methods I have used to count characters, words and check for a palindrome may not be the greatest but I think the main problem is in my main method. The program is supposed to read each line and count the number of characters & words, make a copy of the line, removing every character that isn't a letter and convert it to lower case (for palindrome use), then take the copy and see if its a palindrome. The problem is that the program should stop when it reaches an end-of-file condition or when it reads a line consisting of only a period character. My program unforntuately takes the line that consists of a period character and still checks if its a palindrome and does the count of words & characters. Also, I need to create a "report" at the end with the number of lines, words, characters and how many lines came out to be a palindrome but I have no idea how to start this. Thank you and I hope someone could help me with this one!
Explanation / Answer
please rate - thanks
you were only checking for an EOF, not a single period
only changes made were in main--as you suspected
file tested with and output
import java.io.*;
public class Aa {
/* Counts number of characters in each line
(newline counts as a character) */
public static int countChars(String str4) {
return str4.length() + 1;
}
/* Counts number of words */
public static int words(String str2) {
int wordCount = 0;
String[] words2 = str2.split(" ");
wordCount += words2.length;
return wordCount;
}
/* Checks if the input line is a palindrome */
public static boolean isPalindrome(String str3) {
int left = 0;
int right = str3.length()-1;
while (left < right)
{
if (str3.charAt(left) != str3.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
BufferedReader br = null;
int lines=0,wordCount,charCount,totalwords=0,totalchars=0,yes=0,no=0;
boolean palin;
/* Reads line by line and does stuff to it */
try {
String currentLine;
br = new BufferedReader(new FileReader("p1-test4.in"));
while ((currentLine = br.readLine()) != null&¤tLine.charAt(0)!='.') {
lines++;
String lettersOnly = currentLine.replaceAll("[\W]", "");
String lowerCase = lettersOnly.toLowerCase();
wordCount=words(currentLine);
charCount=countChars(currentLine);
totalwords+=wordCount;
totalchars+=charCount;
palin=isPalindrome(lowerCase);
if(palin)
yes++;
else
no++;
System.out.println("w: " +wordCount + " c: " + charCount +
", " + palin);
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("total lines: "+lines);
System.out.println("total characters: "+totalchars);
System.out.println("total words: "+totalwords);
System.out.println("total palidrones: "+yes);
System.out.println("total not palindrone: "+no);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.