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.
Related
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.
I completed this java project for a class, but I cannot seem to fix the error I'm getting for the web software that grades it- it's called webcat. I've tried the test input the software suggests for a reference test against my solution, and my output looks exactly the same, but I still lost points for this error-
"Error in method main of class Event: Line number 2 of your output is incorrect (line numbers begin at 1). Your main method does not print the correct output when the input is "This is a short test " (input less than 26 characters) [] Line matched except underscores: Invalid Event Code_".
How can I fix this error when the expected ouput looks fine? Thanks in advance!
Code:
public class Event {
/**
* accepts coded event info, prints the info back to std output, and
actual cost and prize number.
*
* #param args Command line arguments - not used.
*/
public static void main(String[] args) {
// variables needed
String shrink, event, date, time, section, row, seat;
double price, discount, cost;
int prizeNum;
// accept input
Scanner userInput = new Scanner(System.in);
// format the numbers
DecimalFormat formatNum = new DecimalFormat("$#,##0.00");
// enter input and trim the space
System.out.print("Enter your event code: ");
shrink = userInput.nextLine().trim();
if (shrink.length() < 26) {
System.out.println();
System.out.println("Invalid Event Code");
System.out.println("Event code must have at least 26 characters.");
return;
}
// locates spot in index of code and assigns
event = shrink.substring(25, shrink.length());
date = shrink.substring(0, 8);
time = shrink.substring(8, 12);
section = shrink.substring(19, 21);
row = shrink.substring(21, 23);
seat = shrink.substring(23, 25);
price = Double.parseDouble(shrink.substring(12, 15)
+ "." + shrink.substring(15, 17));
discount = Double.parseDouble(shrink.substring(17, 19));
// calculates final cost
cost = price - (price * (discount / 100));
// random final number
prizeNum = (int) (Math.random() * 1000 + 1);
// prints the data to std output
System.out.println();
System.out.print("Event: " + event + " " + " " + " ");
System.out.print("Date: " + date.substring(0, 2) + "/"
+ date.substring(2, 4) + "/" + date.substring(4, 8) + " "
+ " " + " ");
System.out.println("Time: " + time.substring(0, 2) + ":"
+ time.substring(2, 4) + " " + " " + " ");
System.out.print("Section: " + section + " " + " " + " ");
System.out.print("Row: " + row + " " + " " + " ");
System.out.println("Seat: " + seat);
System.out.print("Price: " + formatNum.format(price) + " " + " " + " ");
// formats discount before print
formatNum.applyPattern("#.#'%'");
System.out.print("Discount: " + formatNum.format(discount) + " "
+ " " + " ");
// formats cost before print
formatNum.applyPattern("$#,##0.00");
System.out.println("Cost: " + formatNum.format(cost));
System.out.print("Prize Number: " + prizeNum);
Output:
Enter your event code: This is a short test
Invalid Event Code
Event code must have at least 26 characters.
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);
}
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.
I need the currentStockLevel for another void Method in java, is there any possibility to get it?
I think no, because of void right?
public void receive (int currentStock)
{
String outLine;
if (currentStockLevel > 0)
outLine = productCode;
{
outLine = ". Current Stock: " + currentStockLevel;
outLine += " Current Stock changed from " + currentStockLevel;
currentStockLevel += currentStock;
outLine += " to " + currentStockLevel;
int storeCost = wholeSalePrice * currentStockLevel;
System.out.println (productCode + ":" + " Received " + currentStockLevel + "." + " Store Cost " + "$" + storeCost + "." + " New stock level: " + currentStockLevel);
}