Removing a user defined element from a user defined string array [duplicate] - java

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have an assignment due in about 6 hours and I really need help. I stayed up all night working on this but I can't seem to figure it out.
Basically what I need to do is, I have a user defined array. I'm supposed to take that array and manipulate that string into a variety of things. I've gotten most of it but I can't seem to remove a user defined element in the user defined string. I've tried using a for loop to try and find the specific character that the user wants to remove but I can't seem to get it to compile or write properly. This is my code so far:
import java.util.Scanner;
import java.util.Arrays;
public class StringManipulator {
public static void main(String[] args) {
String userStr;
Scanner input = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
userStr = input.nextLine();
while (true) {
System.out.println("Enter your command");
System.out.println("Print Reverse");
System.out.println("Replace All");
System.out.println("Replace Single");
System.out.println("Remove");
System.out.println("Quit");
String choice = input.nextLine();
String[] array = userStr.split("");
if (choice.equals("Print Reverse") || choice.equals("print reverse")) { //reverses the string
for(int i = array.length - 1;i >= 0; i --) {
System.out.print(array[i]);
}
System.out.println();
}
else if (choice.equals("Replace All")) { //Replaces all input letters with new letters that user inputs
System.out.println("What letter would you like to replace?");
String ridOf = input.nextLine();
System.out.println("What letter do you want to replace it as?");
String replace = input.nextLine();
String[] newArray = array;
for (int i = 0; i < array.length; i++) {
if(array[i].equals(ridOf)) {
array[i] = replace;
}
}
System.out.println("");
for(int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
System.out.println("");
}
else if (choice.equals("Replace Single") || choice.equals("replace single")) {
System.out.println("Enter the character to replace?");
String ridOf1 = input.nextLine();
System.out.println("Enter the new character");
String replace1 = input.nextLine();
System.out.println("Which " + ridOf1 + " would you like to replace?");
int choice1 = input.nextInt();
}
else if (choice.equals("Remove") || choice.equals("remove")) {
System.out.println("Enter the character to remove");
String ridOf2 = input.nextLine();
char charRemove = ridOf2.charAt(0);
for (int i = 0; i < array.length; i++) {
if(userStr.charAt(i) != ridOf2) {
userStr += userStr.charAt(i);
}
}
}
else if (choice.equals("Quit") || choice.equals("quit")) {
System.exit(0);
}
}
}
}
The only section that i'm actually worried about at this moment is the
else if(choice.equals("Remove")
section of the code.

Give this a shot explanation further down
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
String userStr;
Scanner input = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
userStr = input.nextLine();
while (true) {
System.out.println("Enter your command");
System.out.println("Print Reverse");
System.out.println("Replace All");
System.out.println("Replace Single");
System.out.println("Remove");
System.out.println("Quit");
String choice = input.nextLine();
String[] array = userStr.split("");
if (choice.equalsIgnoreCase("Print Reverse")) { //reverses the string
for(int i = array.length - 1;i >= 0; i --) {
System.out.print(array[i]);
}
System.out.println();
}
else if (choice.equalsIgnoreCase("Replace All")) { //Replaces all input letters with new letters that user inputs
System.out.println("What letter would you like to replace?");
String ridOf = input.nextLine();
System.out.println("What letter do you want to replace it as?");
String replace = input.nextLine();
String[] newArray = array;
for (int i = 0; i < array.length; i++) {
if(array[i].equals(ridOf)) {
array[i] = replace;
}
}
System.out.println("");
for(int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
System.out.println("");
}
else if (choice.equalsIgnoreCase("Replace Single")) {
System.out.println("Enter the character to replace?");
String ridOf1 = input.nextLine();
System.out.println("Enter the new character");
String replace1 = input.nextLine();
System.out.println("Which " + ridOf1 + " would you like to replace?");
int choice1 = input.nextInt();
}
This is the section I changed .equalsIgnoreCase compares them ignoring case as expected. Then I made sure the user only entered one char and if more ignore them. Then changed the char remove to be a string of only the first char(because you cannot replace with a char it needs to be a string) then I replaced the char to be removed in the string with nothing essentially removing it and set the user string to the new user string
else if (choice.equalsIgnoreCase("Remove")) {
System.out.println("Enter the character to remove");
String ridOf2 = input.nextLine();
String charRemove = String.valueOf(ridOf2.charAt(0));
userStr = userStr.replaceAll(charRemove, "");//This will replace the first char the enter with nothing
}
End of section
else if (choice.equals("Quit") || choice.equals("quit")) {
System.exit(0);
}
}
}
}

String's replace should do it -
public class Test
{
public static void main(String [] args) {
String s = "Hello World";
s = s.replace(" ", "");
System.out.println(s);
}
}

Related

Scan a number of names, print names (number==amount of names) and print Hello to each of them

I am new to Java and have a task: Scanner a number of "strangers' " names, then read these names and print "Hello+name" to the console. If number of strangers is zero, then print "Looks empty", if the number is negative, then print "Looks negative to me".
So the input and output to console should look like this:
3
Den
Ken
Mel
Hello, Den
Hello, Ken
Hello, Mel
So I have this code edited from someone with some related task, but it seems I miss something as I am new to Java...
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of an Array");
int num = input.nextInt();
while (num==0) {
System.out.println("Oh, it looks like there is no one here");
break;
} while (num<0) {
System.out.println("Seriously? Why so negative?");
break;
}
String[] numbers = new String[num];
for (int i=0; i< numbers.length;i++) {
System.out.println("Hello, " +input.nextLine());
}
With using do while loop you can ask the number to the user again and again if it is negative number.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int num;
String name;
Scanner scan = new Scanner(System.in);
//The loop asks a number till the number is nonnegative
do{
System.out.print("Please enter the number of strangers: ");
num = scan.nextInt();
if(num<0) {
System.out.println("It cannot be a negative number try again.");
}else if(num==0) {
System.out.println("Oh, it looks like there is no one here");
break;
}else{
String[] strangers = new String[num];
//Takes the names and puts them to the strangers array
for(int i=0;i<num;i++) {
System.out.print("Name " + (i+1) + " : ");
name = scan.next();
strangers[i] = name;
}
//Printing the array
for(int j=0; j<num; j++) {
System.out.println("Hello, " + strangers[j]);
}
break;
}
}while(num<0);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of an Array");
int num = input.nextInt();
if(num==0) {
System.out.println("Oh, it looks like there is no one here");
}
else if(num<0) {
System.out.println("Seriously? Why so negative?");
}
else {
String numbers[] = new String[num];
input.nextLine();
for (int i=0; i< num;i++) {
numbers[i]=input.nextLine();
}
for (int i=0; i< numbers.length;i++) {
System.out.println("Hello, " +numbers[i]);
}
}
}
}
This is how your code will look and you'll need to add member function input.nextLine(); to read newline character, so there can't be problem regarding input

How can i make my Programm accept Palindrom with capital letter or space between? [duplicate]

This question already has answers here:
Java, Check if a String is a palindrome. Case insensitive
(5 answers)
Closed 1 year ago.
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.next();
String org_word = word;
word = word.replace(" ","");
String reverse = "";
for (int i = word.length()-1; i >=0; i--) {
reverse += word.charAt(i);
}
boolean palindrome = true;
for (int i= 0; i < word.length(); i++){
if(word.charAt(i) != reverse.charAt(i)){
palindrome = false;
}
}
if (palindrome) {
System.out.println("Your word is a palindrome!");
}
else System.out.println("Your word is not a palindrome!");
}
}
If I put a Palindrome in my Program like "racecar" it does it correctly, but if I type "race car" with a space, it doesn't work. Neither does it when I start a word with a capital letter.
You are using scanner.next() to read in your arguments. In the case of a race car, this means it will read in the first word: race. Which is indeed not a palindrome. To solve this, you need to use scanner.nextLine() to read everything until the next line.
For ignoring case sensitivity, you could change all input to lower case. The string method has a very usefull out of the box method: toLowerCase()
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.nextLine();
word = word.replace(" ", "");
word = word.toLowerCase();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverse += word.charAt(i);
}
boolean palindrome = true;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) != reverse.charAt(i)) {
palindrome = false;
}
}
if (palindrome) {
System.out.println("Your word is a palindrome!");
} else {
System.out.println("Your word is not a palindrome!");
}
}

How can I replace the letters in a String?

I am supposed to write code that replaces a letter in an input. For example, if the word is "hello" and the letter to replace is "l" and put "y" it would make "heyyo". I just don't know what to do after the user inputs.
import java.util.Scanner;
public class Letter
{
public static void main(String[] args)
{
// Ask the user for 3 things: their word, letter they want to replace,
// and replacing letter.
Scanner input = new Scanner(System.in);
System.out.println("Enter your word:");
String word = input.nextLine();
System.out.println();
System.out.println("Enter the letter you want to replace:");
String letter = input.nextLine();
System.out.println();
System.out.println("Enter the replacing letter:");
String replace = input.nextLine();
System.out.println();
// Call the method replaceLetter and pass all 3 of these items to it for
// string processing.
}
// Modify this method so that it will take a third parameter from a user that is the String
//they
//want
//to replace letterToReplace with. This method should return the modified String.
public static int replaceLetter(String word, String letterToReplace, String replacement)
{
int count = 0;
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i, i+1).equals(letterToReplace))
{
count++;
}
}
return count;
}
}
Try doing the next, replacing the char at the position if it is the same as the letter to replace.
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) == letterToReplace)
{
word = word.substring(0, i)
+ replacement
+ word.substring(i + 1);
count++;
}
}
Or you could just do the next:
word = word.replace(letterToReplace, replacement);
YCF_L is correct, the best way would be with replace. If you need to do things programatically for some reason, this will work:
public static void main(String[] args)
{
// Ask the user for 3 things: their word, letter they want to replace,
// and replacing letter.
Scanner input = new Scanner(System.in);
System.out.println("Enter your word:");
String word = input.nextLine();
System.out.println();
System.out.println("Enter the letter you want to replace:");
String letter = input.nextLine();
System.out.println();
System.out.println("Enter the replacing letter:");
String replace = input.nextLine();
System.out.println();
// Call the method replaceLetter and pass all 3 of these items to it for
// string processing.
System.out.println(replaceLetter(word, letter, replace));
}
// Modify this method so that it will take a third parameter from a user that is the String
//they
//want
//to replace letterToReplace with. This method should return the modified String.
public static String replaceLetter(String word, String letterToReplace, String replacement)
{
//Short way:
String wayOne = word.replace(letterToReplace, replacement);
//Long way:
String wayTwo = "";
for(int i = 0; i < word.length(); i++){
if(word.charAt(i) == letterToReplace.charAt(0)){
wayTwo += replacement;
} else {
wayTwo += Character.toString(word.charAt(i));
}
}
return wayOne + " VS " + wayTwo;
}

using charAt in a program

What I am trying to do is very elementary. The user should enter a name and the program prints only the initials of the name except for the last word.
Eg:
input - "Mansha Mannan UL Haque"
output - "M.M.U.Haque"
The program is as follows but it does not compile.
class joke
{
public static void main(String str)
{
String alter=" "+str;
int n=alter.length();
for(int i=0;i<=n;i++)
{
char f=alter.charAt(i);
if (f.compareTo(" ")>0)
{
System.out.println(alter.charAt(i+1));
}
}
}
}
The error showed is char cannot be dereferenced.
Split your input string into string array then access each word and print its first char, for last word print it whole.
class joke
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String alter=sc.nextLine(); //take input from user
String arr[] = alter.split(" ");
int n=arr.length(); //total words in user string
for(int i=0;i<n;i++)
{
if(i==n-1){ //if last word
System.out.print(arr[i]);
}
else{
System.out.print(arr[i].charAt(0)+". ");
}
}
}
}
This should do the trick
in this case namewould be your input
String name = "Mansha Mannan UL Haque";
String[] nameArray = name.split(" ");
String newName = "";
for(int i = 0; i < nameArray.length; i++)
{
if(i != nameArray.length -1)
{
newName += nameArray[i].substring(0, 1) + ".";
}
else
{
newName += nameArray[i];
}
}
and you should also make your class joke public and change String str to String[] args

My program is not ending

My program does not end. I am a beginner and am having trouble understanding why. It was working fine before I changed the name, so I copied it to another file, but it still does not end.
import java.util.Scanner;
public class Fan
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
//first input
System.out.println("Enter your first input: ");
String first = s.nextLine();
String[] firstsplit = first.split(", ");
//second input
System.out.println("Enter your second input: ");
String second = s.nextLine();
String[] secondsplit = second.split(", ");
//third input
System.out.println("Enter your third input: ");
String third = s.nextLine();
String[] thirdsplit = third.split(", ");
//fourth input
System.out.println("Enter your fourth input: ");
String fourth = s.nextLine();
String[] fourthsplit = fourth.split(", ");
//fifth input
System.out.println("Enter your fifth input: ");
String fifth = s.nextLine();
String[] fifthsplit = fifth.split(", ");
for (int a = 0; a<=firstsplit.length-1; a++)
{
//skipping over values that say how many pieces are on board
for (int i = 3; i <= 12; i++)
{
//compatible with piece numbers up to 12(max)
if (Integer.parseInt(firstsplit[0])==i) {
while (i >= 1 && i <= Integer.parseInt(firstsplit[i])) {
continue;
}
System.out.println(firstsplit[i]);
}
}
}
}
}
I would be grateful for any advice.
The problem is here:
while (i >= 1 && i <= Integer.parseInt(firstsplit[i])) {
continue;
}
This is an infinite loop since you never change the value of i inside it. Just comment it to make your application finish. Then, spend some time thinking about how to fix it or what are you trying to accomplish with this loop.

Categories

Resources