How can I solved the java.lang.ArrayIndexOutOfBoundsException: ERROR? [duplicate] - java

This question already has answers here:
Unknown cause of: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 from Command Line
(2 answers)
Invoking Java main method with parameters from Eclipse
(6 answers)
Eclipse command line arguments
(4 answers)
How to pass console arguments to application in eclipse?
(8 answers)
Closed 6 days ago.
How can I get this resolved? This is a code, and every time I try to run it this error shows up "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at IntOps.main(IntOps.java:5)"
public class IntOps {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
int prod = a * b;
int quot = a / b;
int rem = a % b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + prod);
System.out.println(a + " / " + b + " = " + quot);
System.out.println(a + " % " + b + " = " + rem);
System.out.println(a + " = " + quot + " * " + b + " + " + rem);
}
}
Please, help me to understand why it is not working. Thanks.

Related

Java (What is Wrong in this Code) [duplicate]

This question already has answers here:
ArrayIndexOutOfBoundsException when launching a java program
(2 answers)
Closed 4 years ago.
I am New to Java Coding if i run below Code the Error is Coming.. Please Help
public class LeapYear {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
int prod = a * b;
int quot = a / b;
int rem = a % b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + prod);
System.out.println(a + " / " + b + " = " + quot);
System.out.println(a + " % " + b + " = " + rem);
System.out.println(a + " = " + quot + " * " + b + " + " + rem);
}
}
O/p:::
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at LeapYear.main(LeapYear.java:4)
If you're running this on netbeans, you can pass arguments into the main method by right clicking on the project, pick properties (it's at the bottom), click on the run category and then underneath Runtime Platform and Main Class is a text field to enter arguments.
If you're calling it from the command line, e.g. running javac and passing it the main class name, just add two command line arguments after the class name.

Printing out multiple random results

I'm making a random creature generator, its going all nice and dandy, however when it comes to printing the results, it prints the same result 5 times. I tried some different things like using println() multiple times and do while loops, however every time I run the file I just get a bunch of the same results. "a b c d e" are strings that generate the creature
int x = 1;
do {
System.out.println(x +" " +a +" " +b +" " +c +" " +d +" " +e);
x++;
} while (x<=5);
The reason why you're getting the same answer 5 times is because your do-while loop runs 5 times without changing the 'creatures' .
System.out.println(a +" "+ b + " " + c + " " + d + " " +e);
If you remove the do-while loop you'll get the same answer only once however just in case i misunderstood your question i made a small demo of a simple way in which to get multiple random results with a for-loop,a String-array and the Random class
String[] creatures = {"Dog", "Cat", "Fish", "Monkey", "Horse"};
Random r = new Random();
for (int i = 0; i < 5; i++) {
String creature1 = creatures[r.nextInt(creatures.length)];
String creature2 = creatures[r.nextInt(creatures.length)];
String creature3 = creatures[r.nextInt(creatures.length)];
String creature4 = creatures[r.nextInt(creatures.length)];
String creature5 = creatures[r.nextInt(creatures.length)];
System.out.println(creature1 + " " + creature2 + " " + creature3
+ " " + creature4 + " " + creature5);
}

What is the output of this code in Java?

I've tried to run the code and I receive the error "point cannot be resolved to a variable". Why am I getting this error and please try running the code yourself and share what the output you're getting is. Thanks.
public class ReferenceMystery1 {
public static void main(String[] args) {
int a = 7;
int b = 9;
Point p1 = new Point(2,2);
Point p2 = new Point(2,2);
addToXTwice(a, p1);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
addToXTwice(b, p2);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
}
public static void addToXTwice(int a, Point b) {
a = a + a;
p1.x = a;
System.out.println(a + " " + p1.x);
}
}
import point class
import java.awt.Point;
replace
p1.x = a;
System.out.println(a + " " + p1.x);
to
b.x = a;
System.out.println(a + " " + b.x);
complete working code is
import java.awt.Point;
public class HelloWorld{
public static void main(String[] args) {
int a = 7;
int b = 9;
Point p1 = new Point(2,2);
Point p2 = new Point(2,2);
addToXTwice(a, p1);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
addToXTwice(b, p2);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
}
public static void addToXTwice(int a, Point b) {
a = a + a;
b.x = a;
System.out.println(a + " " + b.x);
}
}
"Point" class is not available for me to run your program.
Assuming its available and you are able to compile, then, as previous responses indicated...you can't access p1.x within addToXTwice() method, as the input params are 'a' and 'b'.
with "import java.awt.Point", I get the following compilation errors
javac -g ReferenceMystery1.java
ReferenceMystery1.java:18: error: cannot find symbol
p1.x = a;
^
symbol: variable p1
location: class ReferenceMystery1
ReferenceMystery1.java:19: error: cannot find symbol
System.out.println(a + " " + p1.x);
^
symbol: variable p1
location: class ReferenceMystery1
2 errors
These are in alignment with previous comments of "p1" not available from within addToXTwice() method.

Printing a string?

I'm in my first programming class; can anyone help me understand why I can't print my last line please?
package program4;
import java.util.*;
public class Program4 {
public static void main(String[] args) {
int a, b, c, numComparisons;
String comparisons = "Comparisons for triangleType determination: ";
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
}
String triangleType = "";
System.out.print("Enter 3 positive integer lengths for the sides of a "
+ "triangle:");
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
System.out.println("The input lengths are: a = " + a + ", b = " + b + ", and"
+ " c = " + c + "");
if ((a + b < c) || (b + c < a) || (a + c < b)) {
System.out.print("There is no triangle with sides " + a + ", " + b + " and "
+ "" + c + ".");
} else {
numComparisons = 1;
comparisons += "a==b";
if (a == b) {
comparisons += "(T)" + "(b==c)";
numComparisons++;
if (b == c) {
comparisons += "(T)";
triangleType = "Equilateral";
}
} else {
comparisons += "(F)";
if (a == c) {
comparisons += "(T)";
triangleType = "Isosceles";
} else {
comparisons += "b==c";
numComparisons++;
comparisons += "(F)";
if (b == c) {
triangleType = "Isosceles";
} else {
comparisons += "a==c";
numComparisons++;
comparisons += "(F)";
triangleType = "Scalene";
}
}
}
System.out.printf("" + comparisons + (""));
System.out.printf("numComparisons = " + numComparisons);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
}
}
}
Your last line syntax is pretty messed up.
this
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is " + triangleType);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
What IDE/editor are you using? It should should show you the errors here. This should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is" + triangleType);
This is better

I am getting an "incompatible types" error right here and I cannot figure out why

Please help to find where my code is "incompatible type"....I've looked and looked and looked and can't find it anywhere.
import java.util.Scanner;
public class Years
{
private int centuries, decades;
public Years(int years)
{
centuries = years / 100;
years -= 25 * years;
decades = years / 10;
years -= 10 * years;
}
public int getCenturies()
{
return centuries;
}
public int getDecades()
{
return decades;
}
public static void main(String[] args)
{
int years;
if(args.length >= 1)
{
years = Integer.parseInt(args[0]);
}
else
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the amount in years: ");
years = keyboard.nextInt();
keyboard.close();
}
Years y = new Years(years);
System.out.println(years = "y =" +
y.getCenturies() + "c +" + y.getDecades() + "d"); // I am getting the error right here.
}
}
System.out.println(
years = "y =" + y.getCenturies() + "c +" + y.getDecades() + "d"
);
// ^^^^^^^
The problem is years =. The compiler is not really sure what to do with this. The result of the right hand side of the = is a String because you are performing String concatenation.
So the compiler thinks you are doing this:
years = ("y =" + y.getCenturies() + "c +" + y.getDecades() + "d")
years is an int so this is not a valid expression.
You probably just meant this:
System.out.println(
"y = " + y.getCenturies() + "c + " + y.getDecades() + "d"
);
Or possibly to concatenate years somewhere in there:
System.out.println(
years + "y = " +
y.getCenturies() + "c + " + y.getDecades() + "d"
);

Categories

Resources