How to correct this text processing programme? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
import java.util.Scanner;
public class TextProcessing
{
public static void main(String[] args)
{
String sentence, wordToBeTargeted, wordToBeReplaced, output;
boolean wordToCheck;
Scanner myInput = new Scanner(System.in);
sentence = myInput.nextLine();
do
{
wordToCheck = true;
System.out.println("Please enter the word for replacement:");
wordToBeTargeted = myInput.nextLine;
if(sentence.toLowerCase().indexOf(wordToBeTargeted.toLowerCase()) == -1)
{
wordToCheck = false;
System.out.println("This word cannot be found in the sentence!")
}
else
{
System.out.println("Please enter the word you would like to replace with:");
wordToBeReplaced = myInput.nextLine();
}
}while(wordToCheck = false);
}
}
}
Write a file named TextProcessing.java that will replace all occurrences of a word in a string with another word
The expected outcome is like this:

I won't write the code out - you should still get something out of the exercise, but will give some direction. The first approach that comes to mind:
Split the first input on ' ' into a list
Iterate through the list, conditionally changing values based based on two input strings
As you're iterating you can either output into a new string / stringbuilder, or directly write to console depending on the requirements

Related

How do you create a program that will remove from the 2nd character to the space? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to find a way to create a program where users input their full name(first last) and that can remove multiple characters from the second character to another specific character which is the space with StringBuilder. Meaning it will print out the first initial and the entire last name.
Example:
Input:
Barrack Obama
Output:
BObama
You can either use two substrings, which will create two intermediate String objects, or you can use a StringBuilder object as follows:
String input = "Hello everyone, I'm Cho.";
String output = new StringBuilder(input).delete(5, 14).toString(); // "Hello, I'm Cho."
The code below will delete from the second character until the first space detected. For example from Dong Cho to DCho
Scanner scanner = new Scanner(System.in);
String userName;
System.out.println("Enter username");
userName = scanner.nextLine();
int spaceIndex = userName.indexOf(" ")+1;
String firstPartOfString = userName.substring(0, 1);
String lastPartOfString = userName.substring(spaceIndex, userName.length());
userName = firstPartOfString +lastPartOfString;
System.out.println(userName);

trying to split Strings in Java that are scanned from user [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm building a project where I will take three inputs from the users: name,ID,GPA. the users should enter them in one line separated by a semicolumn";" and I want to be able to receive them as one line and be able to save them in three variables.
I'm applying a method where I will take three variables from the user. for example : the user will enter the name,Id and GPA like this:
1;Sally;90.5; //in one line separated by ";"
I want to be able to save each info from the user in different variable.
Can someone tell me how will I be able to implement that?
Here is the method:
private static void addNewStudent() {
System.out.println("enter ID;Name;Gpa; ");
String info = scanner.nextLine();
Note: I'm trying the apply the CSV in my project.
You just need read one line and then split it into string array.The input order must be ID -> NAME -> GPA:
private static void addNewStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("enter ID;Name;Gpa; ");
String info = scanner.nextLine();
if (info != null) {
String[] infoArray = info.split(",");
if (infoArray.length == 3) {
String id = infoArray[0];
String name = infoArray[1];
String gpa = infoArray[2];
}
}
}
This should do to split the input by ";":
String[] input = GPA.split[";"];
Before trying to get the values, check if the input array has the expected size.

How to iterate a for loop for a user input amount of times Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to get a user input number to print out "Color 1 Color 2..." etc. depending on what number the input is.
I want to do this but in java, and I'm not quite sure where to find it.
How to iterate a for loop for a user input in Java?
That will do the work, using only standard java classes (java.io.Console):
import java.io.Console;
public class Consoler {
public static void main(String[] args) {
final Console console = System.console();
console.printf("How may times?\n");
final String line = console.readLine();
try {
final int quantity = Integer.parseInt(line);
for (int i = 1; i <= quantity; i++) {
System.out.printf("Color %d ",i);
}
System.out.println();
} catch (final NumberFormatException e) {
System.err.println(line + " is not a number.");
}
}
}
java.util.Scanner is probably what you're looking for. As for the for loop, you should google Java for loop and read up on how they work. Then combine the two concepts.

Java getString method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have the questions in the top part, but I want to have all the questions at the top, and when I need to ask the questions, I can just pull them down using a variable defined as the question. Right now however, the code is asking the questions from where the questions are, not using the variable "ask" and asking from System.out.print(ask). Any ideas on how to get it to do that?
import java.util.Scanner;
public class Greetings {
public static void main(String[] args) {
Scanner newscanner = new Scanner(System.in);
String ask = getString(newscanner, "Please enter your first name: ");
// String ask2 = getString(newscanner, "Please enter your last name: ");
// String ask3 = getString(newscanner, "Please enter your year of birth:
// ");
}
public static String getString(Scanner newscanner, String ask) {
System.out.print(ask);
String first = newscanner.next();
String firstletter = first.substring(0, 1).toUpperCase();
return firstletter;
}
}
Perhaps what you are looking to do is have the question be printed, and then the answer typed on the line below it? If so, what you need to do is change the first call in getString from System.out.print to System.out.println, which should add on a newline after the question, moving the input to the next line.
EDIT: This is what it might look like now:
Please enter your first name:John
And here's what it would change to:
Please enter your first name:
John

Check a string for consecutive repeated characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It is asked in an interview to write the code in Java to display the string which doesn't have consecutive repeated characters.
E.g.: Google, Apple, Amazon;
It should display "Amazon"
I wrote code to find continues repeating char. Is there any algorithm or efficient way to find it?
class replace
{
public static void main(String args[])
{
String arr[]=new String[3];
arr[0]="Google";
arr[1]="Apple";
arr[2]="Amazon";
for(int i=0;i<arr.length;i++)
{
int j;
for(j=1;j<arr[i].length();j++)
{
if(arr[i].charAt(j) == arr[i].charAt(j-1))
{
break;
}
}
if(j==arr[i].length())
System.out.println(arr[i]);
}
}
}
Logic : Match the characters in a String with the previous character.
If you find string[i]==string[i-1]. Break the loop. Choose the next string.
If you have reached till the end of the string with no match having continuous repeated character, then print the string.

Categories

Resources