Working with integer array in Java [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The output of this array shows a compilation error, if only the a2.SumOfArray({12}, 12);
is commented, it compiles. How do I put it in an array form and get a working output?
Thank you.
import java.util.Scanner;
class Calculation
{
Scanner myScanner = new Scanner(System.in);
int total, I;
int SumOfArray(int data[], int size){
System.out.print("Enter size of array: ");
size = myScanner.nextInt();
for(i=0; i<=size; i++){
System.out.println("Enter number: ");
data[i]=myScanner.nextInt();
total=total+data[i];
}
System.out.println("Sum of Array: "+ total);
return total;
}
public class Main
{
public static void main(String args[]){
Calculation a2 = new Calculation();
**a2.SumOfArray({12}, 12);**
}
}

Simply replace :
a2.SumOfArray({12}, 12);
With
a2.SumOfArray(new int[]{12}, 12);
However, your code has compilation issue try to find those and fix that.

With first glance at your code, there are couple mistakes: (they are not specific to Java8 btw)
your SumOfArray is taking two int for input not array. Change SumOfArray(int data[], int size) to SumOfArray(int[] data, int size) to take a int[] array for input. In java int[] means an int array.
then you need to pass in your array like this a2.SumOfArray(new int[]{1,2,3}, 12); you need the keyword new so the java compiler knows to reserve memory for this new array and you need the int[] to tell the compiler this is an int array.

Related

Java Recursive Method (Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to take an object array and print out the reverse using a recursive method but I'm getting the above listed error. Could someone help me out?
public static void main(String[] args) {
int v = 1;
int x = 2;
String y = "dog";
String z = "cat";
Object[] a = {v, x, y, z};
printReverse(a, a.length);
}
public static void printReverse(Object[] arr, int i) {
if (i > 0) {
System.out.println(arr[i]);
printReverse(arr, i - 1);
}
else {
return;
}
}
}
Your initial call to printReverse needs to pass a.length -1. You’re going out of bounds on the initial call before it ever recurses since arrays are 0-indexed
The error message means you are accessing array element 4 but there are only elements with index 0 to 3. Array indices in Java start at 0.

Trying to pass an array to a different class in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm creating a lottery program and trying linked lists for the first time. I'm passing the numbers array from the Player class to the Lottery class so that I can display the current players Numbers, but it keeps giving me the error "actual and formal parameters differ in length".
There will be another class which will be made for the winning numbers but I'm trying to fix this issue first. I'm unsure how to fix this as I've been stuck for quite a while now.
Any help would be much appreciated.
This is the piece of code I'm having trouble with, this is in the Lottery class in the displayPlayers() method -
for(int i = 0; i < 6; i++) {
System.out.println(currentPlayer.getNumbers(i));
}
public class LOTTERY
{
Scanner keyboard = new Scanner(System.in);
private PLAYER pHead;
public LOTTERY()
{
pHead = null;
}
public void displayPlayers() {
PLAYER currentPlayer = pHead;
System.out.println("Name: " + currentPlayer.getName());
System.out.println("Age: " + currentPlayer.getAge());
currentPlayer.randNum();
System.out.println("Your Numbers: ");
for(int i = 0; i < 6; i++) {
System.out.println(currentPlayer.getNumbers(i));
}
menu();
}
public void runProg() {
displayPlayers();
}
public static void main(String[] args) {
LOTTERY lottery = new LOTTERY();
lottery.runProg();
}
}
The method getNumbers() does not take an parameters but you are trying to pass it i. Additionally, this method returns an array, so you want to get this before the loop iteration, then iterate through it.
You can do this easily using an enhanced for loop:
for (int currentNum : currentPlayer.getNumbers()) {
System.out.println(currentNum);
}
Or with a standard for loop:
int [] arr = currentPlayer.getNumbers();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
The error you are getting says it all, the method you wrote takes no parameters. When you call your getNumbers() method, you pass it the value of i, though java does not expect that, since you declared the method without any parameters.

Can't Pass Int variable through Function in Java that Returns an Int array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The code I have is as follows:
int diceAmount = sInput.nextInt();
int[] dice = dice.rollDice(diceAmount);
The Dice.java class is here:
public static int[] rollDice(int diceAmount) {
int[] dice = new int[diceAmount];
for(int i=0;i < diceAmount;i++){
dice[i] = (int)(Math.random()*6) + 1;
}
return dice;
}
The error that I get is that:
"Cannot invoke rollDice(int) on the array type int[]".
It should work considering all I am doing is passing an integer variable through and setting the returning array to my own dice variable.
It works if I don't set the dice.rollDice(diceAmount); equal to anything but that's not what I want.
Thank you.
int[] dice = Dice.rollDice(diceAmount);
^
This is because the name of your object instante of Dice is dice as well as the integer array. Therefore the compiler deduces that you want to call rollDice on the integer array.
Dice dice = new Dice();
int diceAmount = sInput.nextInt();
int[] diceArray = dice.rollDice(diceAmount);
Considering class Name as Dice so the correct function calling should be
int diceAmount = sInput.nextInt();
int[] dice = Dice.rollDice(diceAmount);
Please note the problem was
that your object name and array name were both same thats why the compiler was giving you error.

Using functions in Java Programming [closed]

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 8 years ago.
Improve this question
I have doubt in using functions in java. I have wrote code for sum of natural numbers using a recursive function but I don't understand the error I am getting. I know it's a silly question though I'm a beginner and I need a brief explanation.
Here is the code
import java.util.Scanner;
public class natural {
public static int main(String args[]){
int a, s = 0,y;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number:");
int x = in.nextInt();
public static int SN(y)
{
if(x==1)
{
return 1;
}
else{
int N = SN(x-1) + x;
return N;
System.out.println("THE SUM IS :"+x);
}
}
Several problems:
You cannot declare a method within a method. Your SN method must be declared outside of the main method.
The parameter y in your SN method must have a type. Based on usage, it is probably supposed to be an int, so the method signature should look like SN(int y).
Despite the method parameter being called y, you appear to be using x everywhere. You should change x to y in the SN method, since that is the label of the data being passed to the method.
As others have pointed out, statements after the return line are unreachable, and as Matt Coubrough said, your IDE is likely warning you about this. Place it before the return line.
Well, one problem here is that you have an unreachable statement. Your System.out.println("THE SUM IS...") is never reached.

how to remove smallest value digit from an integer and return the rest of it without using string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
how to remove smallest value digit from an integer and return the rest of it without using string.Like if we have number 4412,then reomive the 1 and return 442.
I did the coding for extracting the smallest number but dont know how to combine rest of them.
public class RemoveSmallestDigit {
static int testcase1 = 4487;
static int testcase2 = 1111;
public static void main(String args[]){
RemoveSmallestDigit testInstance = new RemoveSmallestDigit();
int result = testInstance.removeSmallestDigit(testcase1);
System.out.println(result);
}
//write your code here
public int removeSmallestDigit(int num){
int small=9;
int digit=0;
while(num!=0){
digit=num%10;
num=num/10;
if(digit<=small){
small=digit;
}
}
System.out.println(small);
return small;
}
}
I suggest that you break this down into steps. For example, you might do something like
Find the digits of a number and store as an array
Find the lowest digit and remove it from the array
Convert the new array back to a number
Of course, you can come up with your own steps if you find something that makes more sense to you. The main idea is to break a problem down into smaller problems. If you have trouble with any of these smaller problems, please come back with more questions.
public int removeSmallerdigit( int a)
{
int smalldigit=0;
int ReverseNumber=0;
int finalNumber=0;
int digit=0;
while(a>0)
{
digit=a%10;
a=a/10;
if(smalldigit>digit)
{
smalldigit=digit;
}
ReverseNumber=ReverseNumber*10+digit;
}
while(ReverseNumber>0)
{
digit=ReverseNumber%10;
ReverseNumber=ReverseNumber/10;
if(smalldigit!=digit)
{
finalNumber=finalNumber*10+digit;
}
}
return finalNumber;
}

Categories