How do you make a program run BOTH of these, first it runs the text file and the
ID: 3917956 • Letter: H
Question
How do you make a program run BOTH of these, first it runs the text file and then prompts the user to input their own.... ALSO HOW DO YOU MAKE THIS INTO A JAVADOC?
https://www.chegg.com/homework-help/questions-and-answers/java-programming-language-write-recursive-method-determine-string-palindrome-create-string-q26809880
1) "Create a String array with several test cases and test your method"
NOTE: Create a file named as 'input.txt' in the same folder/directory as that of your program.
INPUT SNIPPET:
(or) copy the input data:
abaa
abba
abdba
bscsdb
bsbd
JAVA CODE:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Palindrome {
static boolean recurse(String s, int i, int n) {
if (i == n / 2) {
if (s.charAt(i) == (s.charAt(n / 2))) {
return true;
} else {
return false;
}
}
if (s.charAt(i) == (s.charAt(n - i - 1))) {
return recurse(s, i + 1, n);
}
return false;
}
public static void main(String args[]) {
Scanner sc = new Scanner(new File("input.txt"));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines.get(i), 0, lines.get(i).length()) == true) {
System.out.println(lines.get(i) + " is a palindrome");
} else {
System.out.println(lines.get(i) + " is not a palindrome");
}
}
}
}
OUTPUT:
2) "After testing your program with your test cases, allow the user to test your program independently interactively".
NOTE:
Here we will let the user enter the strings him/her self.
JAVA CODE:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Palindrome {
static boolean recurse(String s, int i, int n) {
if (i == n / 2) {
if (s.charAt(i) == (s.charAt(n / 2))) {
return true;
} else {
return false;
}
}
if (s.charAt(i) == (s.charAt(n - i - 1))) {
return recurse(s, i + 1, n);
}
return false;
}
public static void main(String args[]) {
System.out.println("Enter 5 Strings: ");
Scanner sc = new Scanner(System.in);
List<String> lines = new ArrayList<String>();
for (int i = 0; i < 5; ++i) {
lines.add(sc.next());
}
for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines.get(i), 0, lines.get(i).length()) == true) {
System.out.println(lines.get(i) + " is a palindrome");
} else {
System.out.println(lines.get(i) + " is not a palindrome");
}
}
}
}
Explanation / Answer
Palindrome.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Palindrome {
static boolean recurse(String s, int i, int n) {
if (i == n / 2) {
if (s.charAt(i) == (s.charAt(n / 2))) {
return true;
} else {
return false;
}
}
if (s.charAt(i) == (s.charAt(n - i - 1))) {
return recurse(s, i + 1, n);
}
return false;
}
public static void main(String args[]) throws FileNotFoundException {
Scanner sc = new Scanner(new File("D:\input.txt"));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines.get(i), 0, lines.get(i).length()) == true) {
System.out.println(lines.get(i) + " is a palindrome");
} else {
System.out.println(lines.get(i) + " is not a palindrome");
}
}
System.out.println("Enter 5 Strings: ");
Scanner scan = new Scanner(System.in);
List<String> lines2 = new ArrayList<String>();
for (int i = 0; i < 5; ++i) {
lines2.add(scan.next());
}
for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines2.get(i), 0, lines2.get(i).length()) == true) {
System.out.println(lines2.get(i) + " is a palindrome");
} else {
System.out.println(lines2.get(i) + " is not a palindrome");
}
}
sc.close();
scan.close();
}
}
Output:
abaa is not a palindrome
abba is a palindrome
abdba is a palindrome
bscsdb is not a palindrome
bsbd is not a palindrome
Enter 5 Strings:
abaa
abba
abdba
bscsdb
bsbd
abaa is not a palindrome
abba is a palindrome
abdba is a palindrome
bscsdb is not a palindrome
bsbd is not a palindrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.