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);
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 9 days ago.
Improve this question
How can you compare a String coming from a Buffer in Android to a certain string variable? My code is something like this. When I use txtVw.setText(strInput), it shows the string variable strInput, but I cannot compare it to a specific string using an if statement. For example at txtVw the word "CLOSED" is shown but at the if statement it doesn't do anything even if I compared it with a string variable that has a word of "CLOSED".
byte[] buffer = new byte[256];
inputStream.read(buffer);
int i = 0;
for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
}
final String strInput = new String(buffer, 0, i);
txtVw.setText(strInput)
final String str1 = "CLOSED";
if (strInput.equals(str1)){
// *do something*
}
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 1 year ago.
Improve this question
I'll keep this short. I don't know why I'm getting NumberFormatException in my code. Any help would be much appreciated.
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void loadFiles() throws IOException {
Info i = new Info();
Song s = new Song();
int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7;
s.id = i.getSongTxt(a);
s.titulo = i.getSongTxt(b);
String ano = i.getSongTxt(c);
s.id1 = i.getSongArtistsTxt(a);
s.artista = i.getSongArtistsTxt(b);
s.id2 = i.getSongDetailsTxt(a);
String tempo = i.getSongDetailsTxt(b);
String explicita = i.getSongDetailsTxt(c);
String popularidade = i.getSongDetailsTxt(d);
String dancabilidade = i.getSongDetailsTxt(e);
String vivacidade = i.getSongDetailsTxt(f);
String volume = i.getSongDetailsTxt(g);
String[] ano1 = new String[ano.length()];
String[] tempo1 = new String[tempo.length()];
String[] explicita1 = new String[explicita.length()];
String[] popularidade1 = new String[popularidade.length()];
String[] dancabilidade1 = new String[dancabilidade.length()];
String[] vivacidade1 = new String[vivacidade.length()];
String[] volume1 = new String[volume.length()];
ano = ano.replaceAll("\\s+", "");
ano1 = ano.split(" ");
tempo1 = tempo.split("");
for(int n = 0; n < 7; n++){
s.ano[n] = Integer.parseInt(ano1[n]);
}
System.out.println(Arrays.toString(ano1));
}
public static void main(String[] args) throws IOException {
Info i = new Info();
loadFiles();
}
}
and this is what the error looks like.
Exception in thread "main" java.lang.NumberFormatException: For input string: "[2012,2013,2012,2013,2012,2013,2012]"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:660)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at pt.ulusofona.aed.deisiRockstar2021.Main.loadFiles(Main.java:44)
at pt.ulusofona.aed.deisiRockstar2021.Main.main(Main.java:55)
Process finished with exit code 1
Any ideas as to why it's giving me this error?
Read the Javadoc.
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
Your input string "[2012,2013,2012,2013,2012,2013,2012]" is not a number. So that input cannot be processed as a number.
No more specific diagnosis can be made as you have shown too much code that does not matter, not enough code that does matter, and no example data.
I suggest reading the Help section of Stack Overflow to learn how to ask better questions. Specifically: How to create a Minimal, Reproducible Example.
"How do I stop it?"
Look at how you (supposedly) splitting the ano string.
ano = ano.replaceAll("\\s+", "");
ano1 = ano.split(" ");
That says:
Replace all whitespace with ... nothing. That is: remove all whitespace.
Split using a single space as the separator.
Which plainly doesn't make sense. You have removed the very characters that you are telling split to split on!
But that's wrong anyway, because the actual string you are attempting to process clearly also contains ',', '[' and ']' characters, and they shouldn't be part of the number strings.
So what you need to do is redesign the way you are splitting the ano string into a sequence of number strings ... so that it will work with your input.
Hint: find and read the javadocs for String.split(...) and Pattern.
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
}
}
}
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 7 years ago.
Improve this question
I know there has been a similar question before but it doesn't actually solve my problem. I only want to get my string input which represents an employee name and make sure the input is this form "Name". Not "name", not "Name". So i tried to do it but the output doesn't work. So here is the code :
Scanner scanner = new Scanner(System.in);
String ename; // input string
System.out.println("Enter Employee Name : (Type -Name-, not -name-, not -NAME-!!!");
ename = scanner.nextLine(); // read the string input
char[] Transform = new char[ename.length()]; // this array will contain the string split in characters
for (int i = 0;i < ename.length(); i++)
{
Transform[i] = ename.charAt(i); // Split the input to a char array
}
Transform[0] = Character.toUpperCase(Transform[0]); // First Letter Always Capital
for (int i = 1;i < ename.length(); i++)
{
Transform[i] = Character.toLowerCase(Transform[0]); // Other letters small
}
String name = new String(Transform); // convert the array to a new String variable
System.out.println("NEW STRING : " + name );
Output :
You need to change the Statement in the i loop to
Transform[i] = Character.toLowerCase(Transform[i]);
adding Transform[i] and not Transform[0] because you are inserting same first character again and again to the array
Demo
I don't know why you need all this code.
For example:
char[] transform = new char[ename.length()];
for (int i = 0; i < ename.length(); i++) {
transform[i] = ename.charAt(i);
}
is identical to
char[] transform = ename.toCharArray();
And your entire code can be rewritten as:
public static String capitalise(final String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
}
Test cases:
public static void main(String[] args) throws Exception {
System.out.println(capitalise("Name"));
System.out.println(capitalise("name"));
System.out.println(capitalise("NamE"));
System.out.println(capitalise("NAMe"));
}
Output:
Name
Name
Name
Naae
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);