Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have converted my string array to Array using Arrays.toString(array) method.
But still my string looks like an array in this way. [Monday, Sunday, Tuesday]. Okie thats not the issue. I have another array which is also converted to String.
Say [Monday, Tuesday]. Now this is my problem. When I try to use contains method in these arrays it is not working. Can anyone help.
See Arrays#toString(Object[]):
The value returned by this method is equal to the value that would be returned by Arrays.asList(a).toString(), unless a is null, in which case "null" is returned.
and String#contains(CharSequence):
Returns true if and only if this string contains the specified sequence of char values.
Example:
String[] stringArray = new String[] { "Monday", "Tuesday" };
String string = Arrays.toString(stringArray);
boolean result = string.contains("Tuesday");
But I would recommend:
String[] stringArray = new String[] { "Monday", "Tuesday" };
List<String> stringList = Arrays.asList(stringArray);
boolean result = stringList.contains("Tuesday");
See Arrays#asList(T...) and ArrayList#contains(Object)
This is one way to check if one array got (contains) content of another array without using the contains method
String[] string1 = new String[] { "Monday", "Sunday", "Tuesday" };
String[] string2 = new String[] { "Monday", "Tuesday" };
for (int i = 0; i < string1.length; i++) {
for (int j = 0; j < string2.length; j++) {
if (string1[i].equals(string2[j])) {
// do something here
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
Hello i have an array that is filled with values from database and those values i want to assign them to strings i did this code but its making error:
String[] options = s.split(",");
String visible = options[0];
String sms = options[1];
String contact = options[2];
String calllogs = options[3];
I want the values to separate in array and then by index position to delcare them on each string.
You could use this Technique.
String s = "1000";
String[] options = new String[s.length()];
for(int i=0; i< s.length(); i++)
options[i] = Character.toString(s.charAt(i));
But since you are storing chars the array could be declared as char array
You can get the value from your string like this.
String string = "1000";
char[] options = string.toCharArray();
String visible = options[0];
String sms = options[1];
String contact = options[2];
String calllogs = options[3];
or you can directly use charAt() method to get the value at a particular index.
String visible = string.charAt(0);
String sms = string.charAt(1);
String contact = string.charAt(2);
String calllogs = string.charAt(3);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I got this array of string
String s1[] = {"H","E","L","L","O"};
and a method to convert it into int
public static int[] String2Int(String[] k){
int[] arrayBuffer = new int[k.length];
for (int i=0; i < k.length; i++)
{
//arrayBuffer[i] = Integer.parseInt(k[i]);
arrayBuffer[i] = Integer.parseInt(k[i]);
}
return arrayBuffer;
I want the decimal values of the characters but no luck on getting it.
Google is only helping with if the string were a number.
You shouldn't convert it to integer. It should be
arrayBuffer[i] =k[i].charAt(0);
That way you get the ASCII value of char and gets assigned to int.
Edit :
You can also use arrayBuffer[i] = Character.codePointAt(input, 0); as pointed in comments.
Try this:
class MainClass
{
public static void main(String[] args)
{
System.out.println(Arrays.toString(String2Int("HELLO")));
}
public static int[] String2Int(String k)
{
int[] arrayBuffer = new int[k.length()];
for (int i = 0; i < arrayBuffer.length; i++)
{
//arrayBuffer[i] = Integer.parseInt(k[i]);
arrayBuffer[i] = k.charAt(i);
}
return arrayBuffer;
}
}
Output:
[72, 69, 76, 76, 79]
Explanation
char and int is almost the same in Java. You don't need to parse it, you just implicitly cast it by asigning it to the int[]. Also you shouldn't take a String[] as an argument, that makes no sense. A String is already almost a char[]. So either take a String, or a char[]. In my example I've used a String because that is more convenient to call.
Using the Java 8 Stream API:
public static int[] convert(String[] strings)
{
Objects.requireNonNull(strings);
return Arrays.stream(strings)
.mapToInt(str -> Character.getNumericValue(str.charAt(0)))
.toArray();
}
I assume you dont need to check that the input strings contain exactly one character.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
System.out.println("Please input the elements and seperate each by a comma.");
e = dk.nextLine();
String[] elems = new String[e.length()];
st = new StringTokenizer(e,",");
for (int i = 0; i<e.length(); i++) {
elems[i] = st.nextToken().toString();
}
for (int i=0; i<e.length(); i++){
System.out.println(elems[i]);
}
I am trying to print out the array elems[] but it wont work the error java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349 seems to be at line:
elems[i] = st.nextToken().toString();
can you help me identify and understand the problem?
A correct version:
String[] elems = e.split(",");
for(String elem : elems) {
System.out.println(elem);
}
The mistake you made is that e.length() returns the size of the string (its number of characters) so you ended up calling st.nextToken() more times than there are actual tokens separated by ",". Hence the exception.
#Jean posted a slim version of what you are trying, but ultimately to help to understand the error
e = dk.nextLine(); // input: Alfredo,Bauer,Cisco
String[] elems = new String[e.length()]; // length is 20
st = new StringTokenizer(e,","); // st has a length of 3
Now if you call it like this
for(int i = 0;i<e.length();i++){
elems[i] = st.nextToken().toString(); // NoSuchElementException
}
Because you try to call the nextToken() which does not exist.
The docs:
Returns the next token from this string tokenizer.
Throws:
NoSuchElementException - if there are no more tokens in this
tokenizer's string.
To fix your problem use the length of countTokens()
OR
while(st.hasMoreElements()){
elems[i] = st.nextToken().toString();
}
Another alternative.
String[] elems = e.split(",");
System.out.print(Arrays.toString(elems ));
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
Hi for some strange reason i cant remove an item from the ArrayList, it removes the first 4 items but it will not remove City from the list any suggestions would be great. Thanks
String str = getset.getFILEMESSAGE();
ArrayList aList = new ArrayList(Arrays.asList(str.split(",")));
for (int i = 0; i < aList.size(); i++) {
System.out.println(aList.get(i));
System.out.println(aList.size());
}
aList.remove("Person ID");
aList.remove("First Name");
aList.remove("Last Name");
aList.remove("Street");
aList.remove("City");
System.out.println(aList);
System.out.println(aList.size());
String convertedmessage = aList.toString();
System.out.println("converted message = " + convertedmessage);
The code seems to be correct if all entries contain the same content/value. However your comment
i tried changing City 1 that didnt work i need the 1 in my list for the person id heres the string Person ID,First Name,Last Name,Street,City 1,Ola,Hansen,Timoteivn,Sandnes 2,Tove,Svendson,Borgvn,Stavanger 3,Kari,Pettersen,Storgt,Stavanger
implies, that the values are changing. The anwser of JajaDrinker looks correct but if you want to keep it really simple I would just remove the entries by their index. Since they are all at the beginnig, the following code will delete the first five entries of your list and should always do the job.
for(int i = 0; i < 5; i++)
aList.remove(0);
You should probably remove the strings you don't need before splitting the array... like this ...
String str = getset.getFILEMESSAGE();
String strWithOutIDNameStreetCity = str.substring(str.indexOf("City")+"City".length());
Then split it and it contains only the stuff you need.
As you say in your comments the string is:
"Person ID,First Name,Last Name,Street,City,Ola,Hansen,Timoteivn,Sandnes 2,Tove,Svendson,Borgvn,Stavanger 3,Kari,Pettersen,Storgt,Stavanger"
If that is true then your code is working.
The final output with that String would be:
converted message = [Ola, Hansen, Timoteivn, Sandnes 2, Tove, Svendson, Borgvn, Stavanger 3, Kari, Pettersen, Storgt, Stavanger]
So I am pretty sure that there is a problem with the String.
Try using debugger and show us the actual contents of the String during the execution.
String str = getset.getFILEMESSAGE();
ArrayList aList = new ArrayList(Arrays.asList(str.split(",")));
for (int i = 0; i < aList.size(); i++) {
if(aList.get(i).startsWith("City")){
String tmpString = aList.get(i);
String[] stringTab= new String[2];
stringTab= tmpString.split(" ");
aList.get(i) = stringTab[1];
}else if(....){ //Do the same for the others
aList.remove(i);
i--;
}
System.out.println(aList.get(i));
System.out.println(aList.size());
}
System.out.println(aList);
System.out.println(aList.size());
String convertedmessage = aList.toString();
System.out.println("converted message = " + convertedmessage);
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 want to store all possible substring in String []. I tried this but got an error:
public void sub(String word){
String [] Str=new String[100];
int n=0;
for (int from = 0; from < word.length(); from++) {
for (int to = from + 1; to <= word.length(); to++) {
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
}
}
}
What is solution?
error is: cannot find symbol, variable str, loction: class substring
Well, that fairly clearly tells you what the error is: You haven't declared str. You have declared Str, but Java's identifiers are case sensitive, str and Str are not the same identifier.
So change
String [] Str=new String[100];
to
String [] str=new String[100];
// ^--- lower case
Before, when you hadn't said what the error was, there were a couple of other things Pshemo and I (amongst others) noticed:
You have a sequence issue here:
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
...since you're incrementing n before outputting the string, you're always going to output null. Just moving the increment fixes that:
str[n]=word.substring(from, to);
System.out.println(str[n]);
n++;
Another possible problem can occur for longer words, where number of substrings can be more then 100. In that case you should avoid creating fixed size array, but try using dynamic size collection like List
List<String> str = new ArrayList<String>();
To put or read elements here just use str.add(substring) and str.get(index).