so i will get the value of these string from a method, and i need combine them as a file address, but i can't combine the string like i did on FILE_PATH_STRING. I tried to use concat method, but it doesn't work too. FILE_PATH_STRING will always equal to the first string ,which is WORLD_PATH in this case
public static final String WORLD_PATH = "The World/";
public static String CONTINENTS_NAME="";
public static String COUNTRY_NAME="";
public static String FILE_TYPE="";
public static String FILE_PATH_STRING = WORLD_PATH + CONTINENTS_NAME + COUNTRY_NAME + FILE_TYPE;
public static File FILE_PATH = new File(FILE_PATH_STRING);
it should work like, when i click on a map, method will return the name of region to me,and i wills store them in those static string. I tried debug. All of the strings have a value and they correct. but FILE_PATH_STRING only equal to the first string i put in there.
after i run the program,
CONTINENTS_NAME = Asia
COUNTRY_NAME should equal to CONTINENTS_NAME because that's how i set up my file address
FILE_PATH = .png this is method for load map
I am curious, how do you "have four string values from a method" - can you enlighten us on that? Because if it is anything like this:
getStrings(WORLD_PATH, CONTINENTS_NAME, COUNTRY_NAME, FILE_TYPE);
Then that's the problem - the function getStrings() can't modify those strings passed in.
I am only making wild guesses of course, since you haven't given enough information.
Can you do something like:
String path = getWorldPath() + getContinent() + getCountry() + getFileType();
That is, implement four separate methods to get the four separate parts of the path?
Otherwise you will have to define a type which can return all four strings at the same time, or return them in a container like an array:
void test()
{
String[] pathParts = getStrings();
FILE_PATH_STRING = pathParts[0] + pathParts[1] + pathParts[2] + pathParts[3];
}
String[] getStrings()
{
String[] ret = new String[4];
ret[0] = "The world";
ret[1] = "South America";
ret[2] = "Chile";
ret[3] = ".txt";
return ret;
}
Of course, if all you are going to do is concatenate the strings, you could do that in the getStrings() function.
Related
I am relatively new to Java and would like to know how to store variables separately from a single line of user input.
At the minute the user is prompted to enter football results in the following format
home_name : away_name : home_score : away_score
and I am using a while loop to continue to ask user for input until they enter "stop"
(while (input != "stop))
Once the loop is broken I would like my program to output a variety of data such as total games played, but I'm struggling to store the home_name, away_name etc.. especially if the user wishes to enter multiple lines of results.
Two mainstream ways to store a "record" are:
Maps
Data objects
A map is more generic:
Map<String,String> match = new HashMap<>();
match.put("home_name", "Alvechurch Villa");
match.put("away_name", "Leamington");
match.put("home_score", "0");
match.put("away_score", "6");
You can add a map to a list:
List<Map<String,String>> matches = new ArrayList<>();
matches.add(list);
... and retrieve them:
Map<String,String> match = matches.get(0);
System.out.println(match.get("away_score"));
A data object is more tuned to your data format, but you have to write the class yourself.
public class Match {
public String homeName;
public String awayName;
public int homeScore;
public int awayScore;
}
Now you can use this class:
Match match = new Match();
match.homeName = "Studley";
// etc.
You can add and retrieve these from lists too:
List<Match> matches = new ArrayList<>();
matches.add(match);
Match aMatch = matches.get(0);
This is simple, but it's considered bad practice to have public fields like this - it's better to get at them via methods. For brevity, here's a data class with only one field:
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String name() {
return name;
}
}
Player neilStacey = new Player("Neil Stacey");
You can use the same technique with all the fields in Match.
(A common style is to name a method like this getName(), and also to have a setName(). I have used a different style and made the object immutable, in an effort to set a good example!)
One advantage of the data object is that it has different types for different fields: homeName is a String, homeScore is an integer. All the fields in the Map are Strings. You can get around this by using Map<String,Object> but then as a consumer you have to cast to the right type when you read.
String homeName = (String) match.get("home_name");
Data objects allow the compiler to do a lot of compile-time checking that helps you know your code is correct. If you use a map, you won't find out until runtime.
Prompt the user separately for each input.
System.out.println("home_name: ");
String hN = scan.next();
System.out.println("away_name: ");
String aN = scan.next();
System.out.println("home_score: ");
String hS = scan.next();
System.out.println("away_score: ");
String aS = scan.next();
I am not understanding how to use the String.replace() method. Here is the code:
CharSequence oldNumber = "0";
CharSequence newNumber = "1";
String example = "folderName_0";
System.out.println("example = " + example);
example.replace(oldNumber, newNumber);
System.out.println("example.replace(oldNumber, newNumber);");
System.out.println("example = " + example);
And it's outputting:
example = folderName_0
example.replace(oldNumber, newNumber);
example = folderName_0 // <=== How do I make this folderName_1???
The replace method isn't changing the contents of your string; Strings are immutable. It's returning a new string that contains the changed contents, but you've ignored the returned value. Change
example.replace(oldNumber, newNumber);
with
example = example.replace(oldNumber, newNumber);
Strings are immutable. You need to re-assign the returned value of replace to the variable:
example = example.replace(oldNumber, newNumber);
String is a immutable object, when you are trying to change your string with the help of this code - example.replace(oldNumber,newNumber); it changed your string but it will be a new string and you are not holding that new string into any variable. Either you can hold this new string into a new variable, if you want to use your old string value later in your code like -
String changedValue = example.replace(oldNumber,newNumber);
or you can store in the existing string if you are not going to use your old string value later like -
example = example.replace(oldNumber,newNumber);
I have a code to replace stream of string. I need to search a specific string that is defined in the key of properties file
String result="";
int i=0;
while (i<listToken.size()){
result = listToken.get(i);
while (enuKey.hasMoreElements()) {
String key = (String)enuKey.nextElement();
// String value = propertiesSlang.getProperty(key);
if (listToken.get(i).equals(key)){
String value = propertiesSlang.getProperty(key);
listToken.get(i).replace(listToken.get(i), value);
System.out.print("detected");
}
}
i++;
}
But it doesn't replace word. How I can replace words using properties.
It's because you forgot to assign the result, using the method set():
listToken.set(i, propertiesSlang.getProperty(key)));
assuming listToken implements AbstractList
Why complicate things with replace(). As far as I understand your code you can simply do -
String value = propertiesSlang.getProperty(key);
listToken.set(i, value);
I see you have modified your code again to
listToken.get(i).replace(listToken.get(i), value);
Just so that you know String class is immutable. So operations like replace() or substring() will give you a new String and not modify the original one. Get the new String and set it in your list listToken.
I got a String[] which contains of multiple user details. Something like this:
Wilson#$20#$Male=#=Raymond#$25#$Male=#=Sophie#$20#$Female
I wanted to split the string up and organize it into multiple array. Such as one array for Name, one array for Age and another array for Gender. Up to this point I managed to split the String[] into something like this.
String[] User = student.split("=#=");
User[0] = Wilson#$20#$Male
User[1] = Raymond#$25#$Male
User[2] = Sophie#$20#$Female
I don't really know how to organize it from this point. Any comments and answers are highly appreciated!
EDIT
Wilson#$20#$Male=#=Raymond#$25#$Male=#=Sophie#$20#$Female
The above part is actually a value that is returned from the server and I wanted to handle this value. Thank you for all the replies. I think I understand a bit in theory wise, but I'm having slightly issue in implementing codes.
I agree with the suggestions of creating a class for each user - it's the Object Oriented way. So I included it in the example below. But you could probably change it easy enough if you want to do arrays or some other structure.
However, what I want to add is a way to use the Java classes java.util.regex.Pattern and java.util.regex.Matcher to extract both records AND fields in one go from your input string. (I haven't programmed for Android, I assume they are available though.)
The general plan for the pattern is: (record delimiter or nothing)(field1)(delim)(field2)(delim)(lastfield)(record delimiter + rest of input)
The algorithm basically loops through the input with the above pattern. The pattern extracts various groups for the fields (depending on how your record's format) and then also a last group that contains the remainder of the input string. This remainder is used as the new input string for the next loop. So each iteration of the loop does one record.
Here is more complete example code which you can run. You might need to study up on regular expressions to understand the pattern, which is the important part of the algorithm. You can start with the Javadoc for the java.util.regex.Pattern class.
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestPatternMatch {
public static void main(String[] args) {
List<User> result = new ArrayList<>();
String input =
"Wilson#$20#$Male=#=Raymond#$25#$Male=#=Sophie#$30#$Female";
Pattern recPatt =
Pattern.compile("^(=#=|.{0})(.+?)#\\$(\\d+)#\\$(.+?)(?==#=)(.*$)");
// ^match start of line
// ^match record delimiter or nothing
// ^match name field (reluctant)
// ^match field delim
// ^match age field
// ^match field delim
// match gender field^
// zero-width (non recording) lookahead for record delimiter^
// match rest of line until end^
Matcher recMatcher;
String workStr = input; // start with whole input
while (workStr != null && workStr.length() > 0) {
recMatcher = recPatt.matcher(workStr);
if (recMatcher.matches() && recMatcher.groupCount() >= 5) {
result.add(
new User(
recMatcher.group(2), //name
Integer.parseInt(recMatcher.group(3)), //age
recMatcher.group(4) //gender
)
);
workStr = recMatcher.group(5); // use tail for next iteration
} else {
workStr = null;
}
}
System.out.println(result); //show result list contents
}
}
class User {
String name;
int age;
String gender;
/** All argument constructor */
public User(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
/** Show contents of object fields */
public String toString() {
return "User ["+ name + ", " + age + ", " + gender + "]";
}
}
The basic pattern structure can be reused for many different record formats.
Create a User object to store all fields (name, age, gender) and create a list to hold all data.
Your best bet here, is to use an object to hold these values. Objects are the standardized way to hold values that relate to one another, in one Object. ie:
public class Person
{
private String name;
private int age;
private String gender;
// Gender could be a boolean value really, but you've stored it as a String.
}
In the constructor you would request each value and assign it to these fields. It would look something like:
public Person(String name, int age, String gender)
{
this.name = name;
// etc etc
}
That way you have one array, with no need to do any tokenizing of Strings to get to individual values :). You will also need to implement some Accessors and Mutators to get at the values within the Object.
Why not create a User class and maintain a list of User instances.
class User {
String name;
String gender;
int age;
}
The best solution would be to create an class User. If you want to avoid it, try:
String[] User = student.split("=#=");
String [][] details=new String[user.length][3];
String [] temp=new String[3];
for(int i=0;i<User.length;i++){
temp=User.split("//");
for(j=0;j<3;j++){
details[i][j]=temp[j];
}
}
I have two JSON Format Strings
{"user1":{"Iden":4,"nID":1},"user2":{"Iden":5,"nID":1}} // String A JSON
{"user1":{"Iden":4,"nID":1},"user3":{"Iden":6,"nID":1},"user2":{"Iden":5,"nID":1}}
In the below program these above JSON are formatted by Eclipse IDE
This is my program:
import java.util.Map;
import org.codehaus.jackson.type.TypeReference;
import com.tradeking.at.util.JsonHelper;
public class Hi {
private static JsonHelper jsonHelper = JsonHelper.getInstance();
public static void main(String[] args) throws Exception {
Map<String, Tracker> totalCusts = null;
String A = "{\"user1\":{\"Iden\":4,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}";
String B = "{\"user1\":{\"Iden\":4,\"nID\":1},\"user3\":{\"Iden\":6,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}";
String totalString = A+B;
if (null != totalString) {
totalCusts = (Map<String, Tracker>) jsonHelper.toObject(
totalString, new TypeReference<Map<String, Tracker>>() {
});
}
System.out.println(totalCusts);
}
}
Tracker.java:
import org.json.JSONObject;
public class Tracker extends JSONObject{
}
When i ran the above , the Output is
{user1={}, user2={}}
if I use this:
String totalString = B + A ;
The O/p is:
{user1={}, user3={}, user2={}}
Please let me know how I can add two JSON Strings??
At the top-level, a JSON document is always a single object, array, or value. By just concatenating the two strings together, you're violating this principal. A simple workaround would be to join the two values together in an array:
String totalString = "[" + A + ", " + B + "]";
And then parse as such. Or you could simply parse each JSON document one at a time, and then append or merge your results (I suspect you want to merge them, via Map.putAll).
Given that the values for your userN keys are empty, you probably have a bug in your JsonHelper class, but that's hard to say without seeing the code.