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 days ago.
Improve this question
Say for instance I want to repeat the line of code
Integer int1 = new Integer(value1);
for many variables, such as int1 to int100. I am not asking about this exact task in particular - I am asking about any such situation where the code would be identical save for replacing small details like int1 and value1 with int2, value2. Is there a way to have the JVM complete this for me?
I am not even sure what approach to take on this or what term to search for more information. The only thing I can think to try is instead of typing "int1", having a loop that changes a string containing the name and attempting to pass the string as a symbol to the JVM but this of course does not work.
It was a little strange question and I don't know if I understood your meaning correctly or not.
But in this particular case, instead of repeating the code, you can use a data structure like an array. See Oracle tutorial.
int[] numvar = new int[arr.length];
for (int i = 0; i < args.length; i++) {
int someNumber = Integer.parseInt(args[i]);
numvar[i] = someNumber;
}
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 2 years ago.
Improve this question
Good I am trying to create a function that generates new Array, and trying to use a getter method that returns a name for example ArrayD5, ArrayD10 and use these as the name of the new arrays to generate.
I have tried to do this:
int length = 5;
String (SeatIbiza.nombres(length))[] = new String[length];
String nombres (int length) {
return "ArrayD"+length;
}
If you are looking for a way to declare something like e.g.
String "ArrayD" + 2
so that you have a variable
String ArrayD2
then this is not possible in Java. Dynamic naming of variables is not supported.
I don't think if that's possible. The variable names are required to be declared during compile time in JAVA and dynamic naming isn't supported... yet.
Correct me if I'm wrong as I'm still learning about JAVA
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 3 years ago.
Improve this question
So I'm programming a Go Fish game for a project of mine, and I've been at this for a while. Currently I'm trying to check if the player/computer has a full book of a certain number. I've based the whole game off of arrays, and just need to check if the array contains four instances of a given element.
I'm not sure about the specifics but this should get you started.
int count = 0;
for(int i = 0; i < YOURARRAY.length; i++)
{
if( //check to see if the element is the type youre counting)
{
count++;
}
}
if(count == 4)
{
//there are 4 instances
}
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 5 years ago.
Improve this question
I'm trying to make a program that takes the string "1d8" and make the program identify it as int i = (Int) ((Math.Random()*8)+1) one time. It would also be nice if I could make it identify "10d8" to do something like :
for(int i = 1; i <= 10; i++){
int j += (int) ((Math.Random()*8)+1);
}
Thus returning basically the roll of 10 eight sided dice. So my question is how do I get the code to recognize the numbers on either side of the character d and make this work with whatever roll I do.
You can use split method of the String class.
String s = "10d8";
String[] numbers = s.split("d");
numbers[0] will have 10 and numbers[1] will have 8
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
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm
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 7 years ago.
Improve this question
I had recently a discussion about the use of non-counter related conditions in for-loops in Java:
for(int i = 0; o.getC() < 10; i++)
o.addC(i);
Does anyone know if there are any "official" conventions for for-conditions like this? In my opinion it's easier to read compared to an equivalent while-loop because all loop-parameters are together in the first line:
int i = 0;
while(o.getC() < 10) {
i++;
o.addC(i);
}
Or even worse:
int i = 0;
while(o.getC() < 10)
o.addC(++i);
for loops are used in pretty much every situation over equivalent while solution. Arrays, lists, standard data structures.
On the other hand while is commonly used with streams and for infinitely long iterations..
Most developers will expect a for statement to consist of three things:
Initialization of a variable
Termination condition based on the variable
Increment on the variable
If you change your code to contain unexpected things it will get harder to read and thus harder to maintain.
Furthermore, I think the while loop makes your intention clearer: do something while o.getC() is less then 10. This "something" happens to be: add an incrementing number.
Long story short: use a while loop for "non-counter related conditions".
A nice thing about for loops is there is a shortcut method. So if you want to do all of the items in an array you can just do the following:
(double number : arrayName)
where double is the type, number is the name you are giving each element (it doesn't matter really what you call it, you will refer to each value there as "number" in this case). And arrayName is the name of the array you are referring to.
If you want to reach each element/object this is the fastest way.
How about:
for(int i = 0; i < MAX_ITERATIONS; i++)
{
o.addC(i);
if (o.getC() >= 10)
return o; // or break
}
This prevents an infinite loop if addC is not really doing what you expect.