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
String is Like This:
String Text="Bank name Some Thing \n Reminder is Some Thing \n Date Some";
I want it to be Like this:
String T1="Bank Name Some Thing";
String T2="Reminder is Some Thing";
String T3="Date Some";
I'm Working with java
I will be thankful to Your Help.
You can use split() function to split the sentences based on delimiter \n and store them in an array. Then by iterating the array, you can access each string:
public static void main(String[] args) {
String Text="Bank name Some Thing \n Reminder is Some Thing \n Date Some";
String[] sentences = Text.split("\n");
for (String s : sentences){
System.out.println(s.trim());
}
}
Related method's doc page is here.
Since there are additional spaces around delimiter, you can use trim method to remove them.
If you want to assign the values to different variables as mentioned in your question, you can do it as follows:
String T1 = sentences[0].trim();
String T2 = sentences[1].trim();
String T3 = sentences[3].trim();
You should also follow Java naming conventions e.g. the name of the variables should be t1, t2, and t3 instead of T1, T2, and T3.
Another option is to use the Scanner
String text =
"Bank name Some Thing \n Reminder is Some Thing \n Date Some";
Scanner scan = new Scanner(text);
while (scan.hasNextLine()) {
System.out.println(scan.nextLine().trim());
}
Prints
Bank name Some Thing
Reminder is Some Thing
Date Some
If you know how many lines you have you can just assign them directly as follows:
String T1=scan.nextLine().trim();
String T2=scan.nextLine().trim();
String T3=scan.nextLine().trim();
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Let's say I have a String (call it s) with the following format:
[String] [String] [double] [int]
for example,
"YES james 3.5 2"
I would like to read this data into separate variables (a String, a String, a double, and an int)
Note that I come from a C++ background. In C++, I would do something like the following:
std::istringstream iss{s}; // create a stream to the string
std::string first, second;
double third = 0.0;
int fourth = 0;
iss >> first >> second >> third >> fourth; // read data
In Java, I came up with the following code:
String[] sa = s.split(" ");
String first = sa[0], second = sa[1];
double third = Double.parseDouble(sa[2]);
int fourth = Integer.parseInt(sa[3]);
However, I will have to do this to many different inputs, so I would like to use the most efficient and fastest way of doing this.
Questions:
Is there any more efficient/faster way of doing this? Perhaps a cleaner way?
Try it like this. Scanner's constructor can take a string as a data source.
Scanner scan = new Scanner("12 34 55 88");
while (scan.hasNext()) {
System.out.println(scan.nextInt());
}
prints
12
34
55
88
As it has been mentioned in the comments, if this is coming from keyboard (or really from an input stream) you could use Scanner class to do so. However, from input sources other than keyboard, I will not use Scanner but some other method to parse strings. For example, if reading lines from a file, you may want to use a Reader instead. For this answer, I will assume the input source is the keyboard.
Using Scanner to get the input
Scanner scanner = new Scanner(System.in);
System.out.print("Provide your input: ");
String input = scanner.nextLine();
input.close();
Break the String into tokens
Here you have a few options. One is to break down the string into substring by splitting the input using white space as a delimeter:
String[] words = input.split("\\s");
If the order of these substrings is guaranteed, you can assign them directly to the variables (not the most elegant solution - but readable)
String first = words[0];
String second = words[1];
double third = words[2];
int fourth = words[3];
Alternatively, you can extract the substrings directly by using String#substring(int) and/or String#substring(int, int) methods and test whether or not the obtained substring is a number (double or int), or just simply a 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 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 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 6 years ago.
Improve this question
In the following example, this program snippet does not compile, but if we replace the method Character.toString(char) by String.valueOf(char), everything is fine. What is the issue why Character.toString here ? In the documentation, these methods seem to have the same behavior. Thanks for explanation.
public static void main (String args[]) {
String source = "19/03/2016 16:34";
String result = Character.toString(source.substring(1,3));
System.out.print(result);
}
Character.toString(char c) method accepts char value as an argument and you are passing a String class instance which is produced from source.substring(1,3) method. String and char are incompatible types, so compiler can't create correct method call and pass the value
Your code should be rewritten as:
public static void main (String args[]) {
String source = "19/03/2016 16:34";
String result = source.substring(1, 3);
System.out.print(result);
//equivalent to the previous System.out.println call
System.out.print(source.substring(1, 3));
}
Also note that the first substring argument is an inclusive start index, the second one is exclusive end index and the leading index in Java String is 0 (not 1) exactly like in arrays (which is not a coincidence - String characters are stored in char array). So if you want to get a "19" String you should write source.substring(0, 2)
What does the compiler error message say? Anyway, source.substring(1,3) gives you a String while Character.toString() needs a char and does not accept a String.
String.valueOf(source.substring(1,3)) would call String.valueOf(Object), not String.valueOf(char).
You may obtain the same even simpler:
String result = source.substring(1,3);
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
Looking this code
String input="I use this method";
String word=input.replaceAll(" ","/");
char buf[]=word.toCharArray();
but i want to another method to doing this?
This is the easiest way I have found to convert a string including white spaces to a char array in java.
String input = "I use this method";
char[] buf = input.toCharArray();
It looks like you are doing it right, but taking out all of the white space with the replaceAll(" ", "/")
From your question I'm assuming you want to save the string without truncating the whitespace in a char array.
If your remove the line " String word=input.replaceAll(" ","/"); " from your code.Then your code will work perfectly fine.PFB a sample code which might help you to understand this better
public static void main(String[] args)
{
String input="I use this method";
System.out.println(input.length()); //length of string before converting to char array
//String word=input.replaceAll(" ","/");
char buf[]=input.toCharArray();
System.out.println(buf.length);//length of string after converting to char array
for(int i=0;i<buf.length;i++){
System.out.print(buf[i]); /// Print the values in char array
}
}