Integer objects are immutable, so you cannot modify the value once they have been created. You will need to create a new Integer and replace the existing one.
playerID = new Integer(playerID.intValue() + 1);
Answer from Grodriguez on Stack OverflowInteger objects are immutable, so you cannot modify the value once they have been created. You will need to create a new Integer and replace the existing one.
playerID = new Integer(playerID.intValue() + 1);
As Grodriguez says, Integer objects are immutable. The problem here is that you're trying to increment the int value of the player ID rather than the ID itself. In Java 5+, you can just write playerID++.
As a side note, never ever call Integer's constructor. Take advantage of autoboxing by just assigning ints to Integers directly, like Integer foo = 5. This will use Integer.valueOf(int) transparently, which is superior to the constructor because it doesn't always have to create a new object.
Java Integer addition - Stack Overflow
java - Adding integers to an int array - Stack Overflow
java - Operation inside when we add two Integer objects? - Stack Overflow
The method add(Integer) in the type ArrayList<Integer> is not applicable for the arguments (ArrayList<Integer>)
Videos
From the top of my head, you can convert it to string and iterate over each char, accumulating the value with ( charAt( position ) - '0' ). I'm away from a java compiler right now, but I guess this should work. Just make sure you have numerical data only.
int sum = 0;
while(input > 0){
sum += input % 10;
input = input / 10;
}
To add an element to an array you need to use the format:
array[index] = element;
Where array is the array you declared, index is the position where the element will be stored, and element is the item you want to store in the array.
In your code, you'd want to do something like this:
int[] num = new int[args.length];
for (int i = 0; i < args.length; i++) {
int neki = Integer.parseInt(args[i]);
num[i] = neki;
}
The add() method is available for Collections like List and Set. You could use it if you were using an ArrayList (see the documentation), for example:
List<Integer> num = new ArrayList<>();
for (String s : args) {
int neki = Integer.parseInt(s);
num.add(neki);
}
An array doesn't have an add method. You assign a value to an element of the array with num[i]=value;.
public static void main(String[] args) {
int[] num = new int[args.length];
for (int i=0; i < num.length; i++){
int neki = Integer.parseInt(args[i]);
num[i]=neki;
}
}
It's compiled into this:
Integer sum = Integer.valueOf(new Integer(2).intValue()+new Integer(4).intValue());
You can verify this by looking at the byte code disassembly obtained with javap -c.
Here is the part that corresponds to new Integer(2).intValue(), leaving int 2 on the stack:
0: new #2; //class java/lang/Integer
3: dup
4: iconst_2
5: invokespecial #3; //Method java/lang/Integer."<init>":(I)V
8: invokevirtual #4; //Method java/lang/Integer.intValue:()I
Here is the part that corresponds to new Integer(4).intValue(), leaving int 4 on the stack:
11: new #2; //class java/lang/Integer
14: dup
15: iconst_4
16: invokespecial #3; //Method java/lang/Integer."<init>":(I)V
19: invokevirtual #4; //Method java/lang/Integer.intValue:()I
And here the sum 2+4 is calculated with iadd, the sum is boxed into an Integer by a call to Integer.valueOf, and the result is stored in the first local variable (astore_1):
22: iadd
23: invokestatic #5; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
26: astore_1
creation of two Integer instances.
Auto-unboxing of those two instances.
new int that holds the result.
Auto-boxing the result into Integer sum instance.
ListIntegers = [1,2,3,4,5,6,7] value2 = [1,2,3,4,5,6,7] ArrayList<Integer> total = new ArrayList<>();
I'm trying to invoke the method "add" that I have by using this: total = ListIntegers.add(value2);
private ArrayList<Integer> add(ArrayList<Integer> value2) {
ArrayList<Integer> totalAdd = new ArrayList<Integer>();
for (int i = 0; i < ListIntegers.size(); i++) {
//for (int i = ListIntegers.size()-1; i >= 0; i--) {
totalAdd.add(ListIntegers.get(i) + value2.get(i));
}
Collections.reverse(totalAdd);
//array to number
int ans =0;
for (int i = 0; i <= totalAdd.size()-1; i++) {
ans += totalAdd.get(i);
ans *= 10;
}
ans /= 10;
System.out.println(ans);
return totalAdd;
}However, I got the complaint "The method add(Integer) in the type ArrayList<Integer> is not applicable for the arguments (ArrayList<Integer>)"
Any suggestions? Please go easy on me. I'm learning too.
EDIT:
The entire project
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class BigInt {
// variables
private boolean isPositive=true;
private ArrayList<Integer> ListIntegers = new ArrayList<Integer>();
// Consider a String argument to the constructor
public BigInt(String BigIntInput) {
storeStringAsIntegers(setSignAndRemoveItIfItIsThere(BigIntInput));
}
//Consider an integer argument to the constructor
public BigInt(int BigIntInput) {
storeStringAsIntegers(setSignAndRemoveItIfItIsThere(String.valueOf(BigIntInput)));
}
//Consider a BigInt argument to the constructor
public BigInt(BigInt BigIntInput) {
storeStringAsIntegers(setSignAndRemoveItIfItIsThere(BigIntInput.toString()));
}
// Default constructor
public BigInt() {
}
/**
* This method removes the + or - sign if it's the first character of the string,
* and end the program if it's the only character in the string.
*/
public String setSignAndRemoveItIfItIsThere(String input) {
String inputString=input;
if(inputString.length() >= 1) {
if (inputString.charAt(0) == '+' || inputString.charAt(0) == '-') {// if the first character is a sign, delete the sign
if(inputString.charAt(0) == '-')
isPositive=false;
inputString = inputString.substring(1);
}
} else {
throw new InputErrorException("Bad String Input");
}
System.out.println("Set Sign and Remove If It Is There: " + inputString);
return inputString;
}
/**
* This method will work through the string from the end to the front and store the
* string as integers.
* If a character other than a digit is encountered end the program.
*/
public void storeStringAsIntegers(String input) {
String inputString=input;
//for(int i = inputString.length()-1; i >= 0; i--) {
for(int i = 0; i <= inputString.length()-1; i++) {
if(inputString.charAt(i) >= '0' && inputString.charAt(i) <= '9') {
//StringToIntegers = Integer.parseInt(inputString); // Converting the String (inputString) to the primitive type int (StringToIntegers)
//StringToIntegers = StringToIntegers * 10 + (StringToIntegers % 10) ;
//StringToIntegers = StringToIntegers / 10;
ListIntegers.add((inputString.charAt(i) - '0')*1);
//StringToIntegers += (inputString.charAt(i) - '0') * factor;
//factor *= 10;
} else {
throw new InputErrorException("Bad String Input: a character other than a digit is encountered");
}
}
}
/**
* The toString() method creates and returns a String with the sign if it is negative
* and then adding the ArrayList integers starting from the back of the list to the
* front.
* */
public String toString() {
String value = "";
if(isPositive == false)
//value=String.join(value, "-");
value = value + "-";
//for(int i=ListIntegers.size()-1;i>=0;i--) {
for(int i = 0; i <= ListIntegers.size()-1; i++) {
//System.out.println(ListIntegers.get(i));
//value=String.join(value, String.valueOf(ListIntegers.get(i)));
value = value + String.valueOf(ListIntegers.get(i));
}
return value;
}
//*************************************************************************************
//*************************************************************************************
/**
* This method will look through the sign of the input values.
* If both values have the same signs, invoke method add().
* If both values have different sings, invoke method subtract() and set sign to that of larger value.
*/
public BigInt add(BigInt objValue2) {
// 3. https://www.geeksforgeeks.org/add-two-numbers-represented-by-two-arrays/
// use to store the total before converting it into BigInt b3
ArrayList<Integer> total = new ArrayList<>();
// object b3 to store the answer
BigInt b3= new BigInt();
System.out.println();
System.out.println("ListIntegers1: ---> " + ListIntegers);
System.out.println("ListIntegers2: ---> " + objValue2);
ArrayList<Integer> value2 = new ArrayList<Integer>();
// converting objValue2 to String str
String str = objValue2.toString();
System.out.println("String str: ---> " + str);
for (int i = 0; i < str.length(); i++) {
value2.add((str.charAt(i) - 48 ) * 1);
}
value2.remove(0);
System.out.println("ArrayList value2: ---> " + value2);
// Consider the sign and determine whether to use addition or subtraction
// Same signs -> add
// Different sings -> subtract
if((ListIntegers.get(0) == '+' && ListIntegers.get(0) == '+') || (ListIntegers.get(0) == '-' && ListIntegers.get(0) == '-')) {
//total = ListIntegers.add(value2);
} else {
//total = ListIntegers.subtract(value2);
}
return b3;
}
/**
* This method will do the actual calculations; adding
*/
private ArrayList<Integer> add(ArrayList<Integer> value2) {
ArrayList<Integer> totalAdd = new ArrayList<Integer>();
for (int i = 0; i < ListIntegers.size(); i++) {
//for (int i = ListIntegers.size()-1; i >= 0; i--) {
totalAdd.add(ListIntegers.get(i) + value2.get(i));
}
Collections.reverse(totalAdd);
//array to number
int ans =0;
for (int i = 0; i <= totalAdd.size()-1; i++) {
ans += totalAdd.get(i);
ans *= 10;
}
ans /= 10;
System.out.println(ans);
return totalAdd;
}
private ArrayList<Integer> subtract(ArrayList<Integer> value2) {
ArrayList<Integer> ans = new ArrayList<Integer>();
return ans;
}
}Demo:
public class DriverClass
{
public static void main(String[] args)
{
BigInt value1 = new BigInt(1234567);
BigInt value2 = new BigInt(-1234567);
BigInt value3 = new BigInt(-0);
BigInt b3;
b3 = value1.add(value2);
System.out.println("Sum b3 is " + value1 +" + " + value2 + " = " + b3);
}
}
In Java, arrays have fixed size. So if you want a dynamic data structure, you should use classes from Java collections package.
For example, use ArrayList:
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(393993);
intList.add(2918282);
// Then add value when you need it using .add() method
Once you instantiate an array, you cannot add values to it; it becomes fixed in size. You're better off using something like Java.Util.ArrayList which supports adding items beyond its capacity.