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 just declared a two-dimensional array in my program:
String[][] choices = new String[4][];
choices[0] = new String[10];
choices[1] = new String[20];
choices[2] = new String[20];
choices[3] = new String[20];
However, IntelliJ Idea, the IDE I am currently using, threw out a bunch of errors starting from the second line. What is wrong with my code? I have checked other questions regarding two-dimensional arrays and found the exact same syntax being used.
EDIT: Based on my code above, I want the arrays to be of different length. Is that possible to make?
Your code snippet works fine, and you can have inner String arrays of different length, e.g.:
String[][] choices = new String[2][];
choices[0] = new String[1];
choices[1] = new String[2];
choices[0][0] = "Foo";
choices[1][0] = "Bar";
choices[1][1] = "Baz";
System.out.println(choices[0][0] + " " + choices[1][0] + " " + choices[1][1]);
Ideone demonstration.
In short, your problem is elsewhere. Read the errors since that's what they're for. They're often descriptive enough.
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 4 years ago.
Improve this question
I'm working on a calculator app..so i'm thinking is there any way out to store arithmetic operator in an array
Yes, you can store them in a char[] or String[]:
char[] chars = new char[10];
chars[0] = '+';
String[] array = new String[10];
array[0] = "+";
A better way is use some kind of Collection, for example, java.util.List:
List<String> list = new ArrayList<>();
list.add("+");
list.add("-");
list.add("*");
list.add("/");
Technically you cannot store operators in array or in variable.
Instead what you can do is store symbols of arithmetic operators in String or Character arrays.
Which then you can interpret as operators while coding
if(ch == '+')
result = x + y;
This is only useful if the calculator, your making, is implementing an input field where the user can write text.
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 5 years ago.
Improve this question
I need to create a test which will have base URL (https://www.horisen.com) and added part with country code (i.e https://www.horisen.com/se). Problem for me is there are 12 countries to be changed.
I tried to create if loop without any success.
So, in summary, I have to go through all 12 different languages, open those pages in current languages, and continue with next lang. I suppose that I need an array of 12 country codes and call that in a loop, but do not know how to acchieve this.
Thank you in advance
String url = htps://www.horisen.com/
int[] array;
array = new int[6];
array[0] = de;
array[1] = se;
array[2] = dk;
array[3] = fr;
array[4] = en;
array[5] = fi;
for(int i=0; i++) {
do not know how to add after string url country code on the ned
}
Next time, please put some actual java code (that compiles) in your example. Perhaps you're looking for something like this:
String url = "https://www.horisen.com/";
String[] countryCodes = {"de", "se", "dk", "fr", "en", "fi"};
for (String countryCode : countryCodes) {
String countryUrl = url + countryCode;
System.out.println(countryUrl);
}
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
String[] am = new String[10];
am[0] = "Sam";
am[1] = "Sam";
am[2] = "Sam";
am[3] = "Sam";
am[4] = "Sam";
am[5] = "Sam";
am[6] = "Sam";
am[7] = "Sam";
am[8] = "Sam";
am[9] = "Sam";
For a jeopardy game, random questions per GUI Jpanel button click
Approach 1: Shuffling the list
You can shuffle your list using:
List<String> list = Arrays.asList(am);
Collections.shuffle(list);
Even better is starting with an ArrayList, and adding your possibilites to that ArrayList, completely ignoring the String[] array
List<String> list = new ArrayList<String>();
list.add("value1");
list.add("value2");
...
Collections.shuffle(list);
You can then retreive an element from the shuffeled list using get(index), i.e. :
String random = list.get(0);
Approach 2: Randomizing the index
If you insist on using the String[]-array, you can do
String random = am[new Random().nextInt(am.length)]
This will create a Random-object, and generate a random integer ranging from 0 to the length of your array minus one. Once this integer is generated, it will be used as an index for to get an element from your String[]-array.
You can use the same approach on an ArrayList:
String random = list.get(new Random().nextInt(list.size));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string containing this:
D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo.
I want to get just this part:
wt\lifecycle\StateRB
How can I do that?
You can simply spilt whole path to parts and then get the parts you want.
String path = "D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo";
String[] parts = path.split("\\");
parts = Arrays.copyOfRange(parts, parts.length-3, parts.length);
Or you can get throught string using loop (this seems to be better)
int index = 0, i = 0;
Stack<String> al = new Stack<String>();
while((index = path.lastIndexOf()))!=-1 && i < 3) {
al.push((path = path.substring(index)));
i++;
}
String[] parts = (String[])al.toArray(); //if you don't have array elements
// in correct order, you can use
// Collections.reverse with Arrays.asList
// applied on array
You can use string tokeniezer with \ delimiter and fetch only last three string tokens. i hope that above path going to be constant always.
http://www.tutorialspoint.com/java/java_string_substring.htm
Check the above link
example :
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.substring(10) );
System.out.print("Return Value :" );
System.out.println(Str.substring(10, 15) );
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 this string I got from a table via 'string.split(" ");' the problem I have is one of them should include a period. How would I go about searching for this period. I do know I have to search for it because I code in Lua. Though we used String.find() methods.
What is the way I would remove a period from a string in a String[] table?
Thanks in advance :)
This is for school.
If I understand you correctly, you have a String that you've split by delimiting on a space. You now want to search for a period in the resultant array of Strings.
for(String s : stringArray) {
if (s.contains(".")) {
//do something
}
}
Unfortunately, I'm not that clever with RegExp, but you could...
String s = "This.is.a.test";
while (s.contains(".")) {
s = s.replace(".", "");
}
System.out.println(s);
I'm sure there's a wonderful single line RegExp to do the same thing
Updated based on comments
Because String is not mutable, you will need to reassign it back to the original array
String[] apples = {"one", "two.", "three"};
for (int index = 0; index < apples.length; index++) {
String s = apples[index];
while (s.contains(".")) {
s = s.replace(".", "");
}
apples[index] = s;
}
for (String s : apples) {
System.out.println(s);
}