Guys, this is my third time putting the same question! You guys are changing my
ID: 3738420 • Letter: G
Question
Guys, this is my third time putting the same question! You guys are changing my the code on my main method when I have to add a method that lets the user input 5 strings WITHOUT taking off any of this code! I just need add a method to the same code!!
public class Recursionn { 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;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws Exception
{
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");
}
}
}
}
I just need help with this CODE! without taking anything out of it. Just adding a method that lets the user interact with it!
Explanation / Answer
Recursionn.java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Recursionn { 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;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws Exception
{
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");
}
}
interactWithUser();
}
public static void interactWithUser() {
Scanner scan = new Scanner(System.in);
String s[] = new String[5];
System.out.println("Enter the "+s.length+" Strings: ");
for(int i=0;i<s.length;i++) {
s[i]=scan.nextLine();
}
for(int i=0;i<s.length;i++) {
if(Recursionn.recurse(s[i], 0, s[i].length())) {
System.out.println("String "+s[i]+" is a palindrome");
} else {
System.out.println("String "+s[i]+" is NOT a palindrome");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.