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 3 years ago.
Improve this question
Java program to find a character from a sentence and replace it with another character. If the character is not found in the string print "character not found".
Note: Replace only the first occurrence.
Sample input 1:
Enter the string:
java programming
Enter the character to be searched:
a
Enter the character to replace:
o
Sample output 1:
jova programming
Kindly suggest me how to take user input for the character to be replaced and replace the character.
In general to get a input from user you could a scanner class.
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter the character to be searched");
String characterToReplace = myObj.nextLine(); // Read user input
System.out.println("Enter the character to replace");
String replacementCharacter = myObj.nextLine();
Information on scanner class
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Other ways to read input from command line :
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
For replacing a character you could just string.replace method to perform the required operations.
You can achieve this by putting this code in a function:
Scanner readInput = new Scanner(System.in);
System.out.println("Enter the string to search:");
String search = readInput.nextLine();
System.out.println("Enter the character to be searched:");
String find = readInput.nextLine();
System.out.println("Enter the character to replace it with:");
String replace = readInput.nextLine();
if (search.contains(find)) {
return search.replaceFirst(find, replace);
} else {
return "Character not found";
}
Related
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);
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.
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 4 years ago.
Improve this question
I want to use a Class to hold user inputs. For example "example_name";"example_value";"example_city" is the form of the input and I want to make objects from an input stream of theese three inputs line by line from a file. So whenever I enter Adam;27; London it should be saved into an Object(Adam,27,London) and ask for the next input.
Thank you for your help, I know this might be a stupid question but I'm new to OO programming and I have a C background and don't want to use two dimensional arrays.
Let's break this problem into 3 section. First, we will have to design the class. Second, we will take the user input and create an object of that class until the user press n(no). Third, we will display the user information. Here I have used a LinkedList data structure for storing the inputs.
Section 1:
class example{
String example_name;
int example_value;
String example_city;
example(String name,int value,String city)
{
this.example_name = name;
this.example_value = value;
this.example_city = city;
}
}
section 2:
LinkedList<example> ob = new LinkedList<>();
Scanner input = new Scanner(System.in);
System.out.println("Press y to Continue n to Exit");
char i = input.next().charAt(0);
while(i!='n')
{
input.nextLine();
System.out.println("Enter name");
String name = input.nextLine();
System.out.println("Enter value");
int value = input.nextInt();
input.nextLine();
System.out.println("Enter city");
String city = input.nextLine();
ob.addLast(new example(name,value,city));
System.out.println("Press y to Continue n to Exit");
i = input.next().charAt(0);
}
Section 3:
for(example x:ob)
System.out.println(x.example_name + " " + x.example_value + " "+x.example_city);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a question. I am a beginner in programming and I have been stuck here for hours now. I want to know how you can read all the letters in a certain user input.
String naem = inPut.nextLine(); //where inPut is a "Scanner inPut = new Scanner (system.in)
nameInput = naem.charAt(0); //where nameInput is a char
System.out.println("Name: "+nameInput);
I am making a program where have to take input a name and all the characters of that name will be printed through System.out.println("Name: " +nameInput);
I'd really appreciate it if you could help out.
You want to print the naem which you are receiving by the input and not a single character which is what you'd get if you used .charAt().
nameInput = naem.charAt(0); //1
System.out.println("Name: "+nameInput);//2
What actually these two lines are doing is that :
Line 1 is taking the first character of the string and storing it into nameInput and then line 2 is printing it.
Now if you want to print all the character in this way you have to go through the entire string. You can do this by using loop.
Here is an Example :
String naem = inPut.nextLine(); //where inPut is a "Scanner inPut = new Scanner (system.in)
System.out.println("Name: ");
for (int i = 0; i < naem.length(); i++)
{
char nameInput = naem.charAt(i);
System.out.print(nameInput);
}
By naem.length(), you will get the length of the string.
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 8 years ago.
Improve this question
I have an input from user of the following form:
1234 abc def gfh
..
8789327 kjwd jwdn
stop
now if i use Scanner and in turn use
Scanner sc=new Scanner(System.in);
String t=sc.nextLine();
while(!t.equals("stop"))
{
int i=sc.nextInt();
int str=sc.nextLine();
t=sc.nextLine();
}
Is there some way by which i may get
i=1234
str="abc def gfh"
...
and so on...and stop when the user enters a stop
I want to accept the numerical values and strings separately...without using regex.
Also I want to stop taking input with keyword "stop".
First of all, you are doing nothing with the accepted input, just ignoring it to take next input.
Second, scanner.nextLine() returns you the next line read which is String. To get the tokens separately, you would need to split the string read to get them.
Third, you should check in your while, whether you have next input or not using scanner#hasNextLine, if its equal to true, then only you should read your input in your while loop.
If you want to read each token separately, you should better use Scanner#next method, which returns the next token read.
Also, you want to read integers and strings, so you also need to test, whether you are having an integer. You would need to use Scanner#hasNextInt method for that.
Ok, since you want to read integer and string separately on each line.
Here's what you can try: -
while (scanner.hasNextLine()) { // Check whether you have nextLine to read
String str = scanner.nextLine(); // Read the nextLine
if (str.equals("stop")) { // If line is "stop" break
break;
}
String[] tokens = str.split(" ", 1); // Split your string with limit 1
// This will give you 2 length array
int firstValue = Integer.parseInt(tokens[0]); // Get 1st integer value
String secondString = tokens[1]; // Get next string after integer value
}
You never change the value of t so the while condition will be always true unless the first line of your file is stop.
your code:
Scanner sc=new Scanner(System.in);
String t=sc.nextLine();
while(!t.equals("stop"))
{
int i=sc.nextInt();
int str=sc.nextLine();
t=sc.nextLine();
}
First of all int str=sc.nextLine(); is wrong as nextLine() returns string. According to me, what you can do is:
Scanner sc=new Scanner(System.in);
String t=sc.nextLine();
int i;
String str="";
while(!t.equals("stop"))
{
int index=t.indexOf(" ");
if(index==-1)
System.out.println("error");
else{
i=Integer.parseInt(t.substring(0,index));
str=t.substring(index+1);
}
t=sc.nextLine();
}
I hope it helps.