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 6 years ago.
Improve this question
When I run this code:
public class Calc2 {
public static void main(String[] args) {
double result;
result = Double.parseDouble(args[0]) + Double.parseDouble(args[1]);
System.out.print(result);
}
}
I get this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Calc2.main(Calc2.java:4)
You receive this error message because the length of the args array is shorter that the position you are tring to access. As the other answers have mentioned, you may not be sending the command line arguments, or you may be sending only one, and when you try to access the second one (args[1]) you receive Array index out of bounds exception.
The best thing you can do, is check for the length of the array before you access the values, or even iterating inside a for instruction until the length of the array is reached.
Use this instruction to print the values:
for(int i=0; i< arr.length;i++)
{
System.out.print(Double.parseDouble(args[i]));
}
And if it does not show anythng is because you are not passing the arguments to the program.
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 10 days ago.
This post was edited and submitted for review 10 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm starting with recursion in Java, and I'm having a problem with a counter, where I want to print all numbers from 1 to number, but it only prints the value of i. i was defined at the start of the class as:
int i = 1;
I've tried putting
i>=number
number>=i
i>=1
number>=0
number>=1
number>0
number>1
After getting rid of i, I also tried:
public static void ContadorCreciente(int number) {
if (i <= number) {
System.out.println(i);
ContadorCreciente(i++);
}
}
But now, this is what I've written so far:
public static void ContadorCreciente(int number) {
if (number > 1) {
ContadorDecreciente(number - 1);
System.out.println(number);
}
}
Ok, you're implementing ContadorCreciente with one number argument. Recursive, so you're going to call it again with either number+1 or number-1. For the +1 variant you have no way to stop it, that would keep going up forever, so that's no good, the recursive call has to be with -1.
The -1 recursion can stop e.g. at ContadorCreciente(0) which is expected to do nothing. I'm sure you can figure out how to do nothing and return immediately.
Inductive approach:
Assuming we already have ContadorCreciente(number-1) which prints all numbers 1..number-1. How would you use such a function to implement the case ContadorCreciente(number)?
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 7 years ago.
Improve this question
I need to write a program Minesweeper.java that takes 3 command-line arguments M, N, and p and produces an M-by-N boolean array where each entry is occupied with probability p. Where I'm stuck is how to create a two dimensional array where the user inputs what the dimensions are. Any help with that part would be great, thanks :)
Here you go the main method:
public static void main(String args[]) {
int a = Integer.valueOf(args[0]);
int b = Integer.valueOf(args[1]);
int myArray[][] = new int[a][b];
//your code
}
It's java. Chapter 0
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 heard that Java integers are pass by value, so why does the following code work in code.runnable.com?
public class HelloWorld {
public static void main(String[] args) {
int number = 0;
number = 2;
System.out.println(number);
}
}
The code will print out 2.
This code snippet doesn't pass number anywhere. You're declaring a local variable and then overriding its initial value. This is perfectly legal in Java, and has nothing to do with passing by reference or by value.
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
What should I change in my code to write my array into text file in a single line instead of a different line per vale of the array?
PrintWriter zapis = new PrintWriter("wyniki.txt");
for (int g=1; g<801; g++)
{
zapis.println(g+":"+wynik[g]+" ");
}
zapis.close();
As the documentation states, println "Prints a String and then terminates the line."
If you don't want to terminate the line, use print instead.
Change the line inside for loop like this :
zapis.print(g+":"+wynik[g]+" ");
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 am in a a JAVA programming class online and I need some help with making an array. I have asked some fellow class mates to no avail. I need to make write a method using the getArray() method that will return an array of integers with the capacity of 500.
I do know how to write a JAVA statement that initializes an array of integers with a capacity of 500. However, it nots helping me much. (i named the array "values")
int [] values = new int [500];
Please be timely if you answer this. I need to know how to do this before Wednesday 2/5/14 Thank You
public int[] getArray(){
return new int[500];
}