Simple Recursion Issue Back ways - java

public class recursionExcercise4
{
public static void main (String[] args)
{
boolean statement=false;
String ch="";
String a="I am bubbles who is a little slugger and loves apple and yellow."
BacktoBacks(a,ch,statement);
}
public static void BacktoBacks(String sentence, String ch, boolean statement)
{
String newLine="",word="";
System.out.print(sentence.charAt(0));
if(sentence.charAt(0)=='.') System.out.println();
if(sentence.length()>1)
{
int num=sentence.substring(1).indexOf(" ");
word = sentence.substring(0,num);
System.out.println(word);
}
BacktoBacks(newLine,ch,statement);
}
}
That is the code.
The lines inside the if statement loop were added by me so you can change that but nothing else can be changed. The if statement on top must remain there. Also, I am trying to avoid loops as it makes it too easy then. Any way to do this? I tried it but need help.
The objective is to print out the words from the string that have double letters. This should also be printed backwards. So like this:
Yellow
Apple
Slugger
Little
Bubbles
Your help is much appreciated.
Thanks!!

So you need a function to check whether a word contains double letters,
public boolean hasDoubleLetters(String word){
// test
}
and based on its outcome, print the word after the recursive call. And you have to pass the correct argument to the recursive call.

Related

Using a method to find the letter in the inputted integer

"Write and test the method that returns a letter of the alphabet from a given word, it the position is given. (Hint: use the method that begins with static char getLetter (String txt, int n)."
I've been staring at this question for 20 minutes, can't seem to understand what it wants me to do.
This is what I have so far:
// The "Divide_raminAmiri" class.
public class Divide_raminAmiri
{
public static void main (String[] args)
{
String word;
int location;
System.out.println ("Enter a word.");
word = In.getString ();
System.out.println ("Enter the location of the letter.");
location = In.getInt ();
} // main method
public static void test (char c)
{
System.out.println (word.charAt (location));
}
} // Divide_raminAmiri class
I'm confused. I think what it wants me to do is use methods to find the letter at the location provided, but I'm getting errors. Any help appreciated!
Okay so I'm not gonna give the full solution since this seems to be some kind of an exercise.
What might help you:
The way you are doing it, you can't access the variable word in the test-method because it is only visible to the main-method, that's why we use parameters so you can pass variables to other methods.
Speaking of parameters, your method is asked to have two parameters, one being the String and one being an int, your test-method only has a char as parameter (?)
Your program starts and ends in the main-method, since you don't call your test-method there, it never gets executed.
Hints for your actual problem:
You can get a char at position x from a String with the method
char myChar = myString.charAt(x);
A char can be cast to an int with
int asciiValue = (int) myChar;
My last hint: Big letters have an ASCII-Value starting at 65 (='A'), small letters of 97 (='a').
Hope that helped, if you got any more questions feel free to ask.

Recursive Palindrome Checker

This method only works for small inputs such as xox but not with a more complex input like taco cat. I have read this code repeatedly and have not been able to fix the problem. I assume there is a tiny error as I have changed the code structurally trying to tweak my approach and have not been able to fix it.
import java.util.Scanner;
public class Palindromes
{
static Scanner scan = new Scanner(System.in);
public static void main (String[] args)
{
System.out.println("Enter a string, human:");
String s=scan.nextLine();
if(palindrome(s)){
System.out.print("This is a palindrome, I am amused Earthling.");
}else{
System.out.print("Don't you know to speak only in palindromes to your alien Overlord?");
}
}
public static boolean palindrome(String s){
s.replace(" ","");
if(s.length()<2){
return true;
}else if(s.charAt(0)==s.charAt(s.length()-1)){
return palindrome(s.substring(1,s.length()-2));
}else{
return false;
}
}
}
Two things to fix:
You forgot to assign the result of replace back to s, resulting in you ignoring the result with spaces removed. Try:
s = s.replace(" ","");
You have an off-by-one error when taking the substring to pass to the recursive call. The ending index of substring is exclusive, so you are trimming one too many characters off the end of the substring. Try:
return palindrome(s.substring(1,s.length()-1));

Censored Words Condition

I need this program to print "Censored" if userInput contains the word "darn", else print userInput, ending with newline.
I have:
import java.util.Scanner;
public class CensoredWords {
public static void main (String [] args) {
String userInput = "";
Scanner scan = new Scanner(System.in);
userInput = scan.nextLine;
if(){
System.out.print("Censored");
}
else{
System.out.print(userInput);
}
return;
}
}
Not sure what the condition for the if can be, I don't think there is a "contains" method in the string class.
The best solution would be to use a regex with word boundary.
if(myString.matches(".*?\\bdarn\\b.*?"))
This prevents you from matching sdarnsas a rude word. :)
demo here
Try this:
if(userInput.contains("darn"))
{
System.out.print("Censored");
}
Yes that's right, String class has a method called contains, which checks whether a substring is a part of the whole string or not
Java String Class does have a contains method. It accepts a CharSequence object. Check the documentation.
Another beginner method would be to use the indexOf function. Try this:
if (userInput.indexOf("darn") > 0) {
System.out.println("Censored");
}
else {
System.out.println(userInput);

Simple Substring Issue

My teacher wants us to make a letter 'o' move around the console. The letter 'o' has been coded to appear in the center of the console screen. I have already created the movingRight and movingDown methods but I'm having difficulty creating the movingLeft and movingUp methods. Here is my code:
import java.util.Scanner;
public class Main {
static String letter = "\n\n\n\n O";
String whenmovingup = letter.substring(0, 1);
char whenmovingleft = letter.charAt(letter.length() - 2);
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(letter);
input.nextLine();
if (input.equals("left")) {
movingLeft();
}
if (input.equals("right")) {
movingRight();
}
if (input.equals("up")) {
movingUp();
}
if (input.equals("down")) {
movingDown();
}
}
public static void movingRight(){
letter = " " + letter;
}
public static void movingDown(){
letter = "\n" + letter;
}
public static void movingLeft(){
letter.remove(whenmovingleft);
}
public static void movingUp(){
letter.remove(whenmovingup);
}
}
I'm having an issue with removing the whenmovingfeft and whenmovingup substrings from my original string letter. It's giving an error ('The method remove(char) is undefined for the type String'), and I'm not sure what needs to be done.
Does anyone know how this can be resolved?
Thanks in advance for all responses.
There is no remove method for a string. However, there is a replace method that may do what you want. Note that it does not modify the string object, but it returns a new string. So you would do:
letter = letter.replace(whenmovingup, "");
Note that there are two slightly different overloads of replace which do different things depending on whether you ask it to remove a String or char. The replace(String, String) method replaces one occurrence, while the replace(char, char) replaces all occurrences. You want just one, so declare whenmovingleft as a String and initialise it appropriately.

Debugging variable error with Java

I am trying to do this stuff. If a user enters "C:\Windows\system32\foo.txt" then the program will convert it to "C:\\Windows\\system32\\foo.txt". A front slash needs to be added to every other preceding slash. Here's what I have coded till now (only the section relevant):
import javax.swing.*;
public class test {
public static void main(String[] args){
String path = JOptionPane.showInputDialog(null, "Enter the File path", "Word counter", JOptionPane.INFORMATION_MESSAGE);
for (int z=0;z<=path.length()-1;z++)
{
if (path.charAt(z) == '\\')
{
path.charAt(z) = "\\\\";
}
}
System.out.println(path); // For knowing what's going on
}
}
Unfortunately it's not compiling, and I don't have a clue of what to do. Any possible help welcomed. Thank you!
You are trying modify a String. Remember strings are immutable.
you can try something like
path.replace(oldChar, newChar) if you want to replace some chars.
This: path.charAt(z) cannot be on the left side of an assignment statement. Instead concatenate your String or use a StringBuilder.
Or just use String's replace(...) method.

Categories