Calling method on an object, and using same object as parameter for method - java

I am working on a java program for creating a slot machine. The program works how I want it to but I am not sure if one of my method calls is proper java etiquette. In my main method below, inside my for loop, I call the method rollAndCompare() on the FourTumblers object, machine. This method returns an integer, coin, which represents how much the user won based on the number of tumblers matched. This if-else statement is written in the FourTumblers class. However, I also pass the same machine object as a parameter so that the method can access the tumbler values of the object. Is there a better way to do this? Is this correct?
public static void main(String[] args) {
int coins;
int addtLives;
int bank = 0;
int lives = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter how many games you want to play:");
int num = scan.nextInt();
System.out.println("You have decided to play " + num + " games.\n");
for (int i = 1; i <= num; i++) {
FourTumblers machine = new FourTumblers();
coins = machine.rollAndCompare(machine);
bank += coins;
addtLives = coins/100;
lives += addtLives;
System.out.println("You won " + coins + " coins. That's " + addtLives + " lives.");
System.out.println("You now have a total of " + bank + " coins and " + lives + " lives.\n");
}
scan.close();
}
Here is my rollAndCompare method...
public int rollAndCompare(FourTumblers machine) {
value1 = machine.getValue1();
value2 = machine.getValue2();
value3 = machine.getValue3();
value4 = machine.getValue4();
if ((value1 == value2)&&(value2 == value3)&&(value3 == value4)){
System.out.println(value1 + " | " + value2 + " | " + value3 + " | " + value4);
System.out.println("Jackpot!");
coins = 600;
return coins;
}
else if (((value1 == value2)&&(value2 == value3))||((value1 == value3)&&(value3 == value4))||((value1 == value2)&&(value2 == value4))||((value2 == value3)&&(value3 == value4))){
System.out.println(value1 + " | " + value2 + " | " + value3 + " | " + value4);
coins = 300;
return coins;
}
else if ((value1 == value4)||(value1 == value2)||(value1 == value3)||(value2 == value3)||(value2 == value4)||(value3 == value4)){
System.out.println(value1 + " | " + value2 + " | " + value3 + " | " + value4);
coins = 100;
return coins;
}
else{
System.out.println(value1 + " | " + value2 + " | " + value3 + " | " + value4);
coins = 0;
return coins;
}
}

The times you would want to add an object onto itself in a class is if you are going to compare it with other objects from the same class.
Ex:
public class Sample
{
private String name = "Sample Chocolate";
private int cost = 1;
public boolean compareSample(Sample sample)
{
if (sample.name.equals(name) && cost == sample.cost)
return true;
else
return false;
}
Then from another class:
public class SampleTester
{
public static void main(String[] args)
{
Sample sample1 = new Sample();
Sample sample2 = new Sample();
System.out.println("Are sample one and sample two the same?: " + sample1.compareSample(sample2));
}
}
Otherwise you can simply put nothing inside the parenthesis:
public boolean compareSample()
{
if (name.equals("Sample Chocolate") && cost == 1)
return true;
else
return false;
}
As Ashiquzzman eloquently said: You can call that using this.getValue(), you don't need to pass machine (to be used as machine.getValue()).

Related

How can i shorten this code and make it less repetitive?

I made a simple calculator but the if statements are very repetitive and long. I am wondering what other solution I could use to shorten it and make it less repetitive. For example using a method (which i have tried but not succeeded) or any other techniques that are usable. Preferably not too advanced since I'm a beginner.
import static java.lang.System.*;
import static javax.swing.JOptionPane.*;
import static java.lang.Integer.*;
public class SimpleCalc {
public static void main(String[] args) {
String operator = showInputDialog("Choose operation: " + "\n" +
"[1] = Plus" + "\n" +
"[2] = Minus" + "\n" +
"[3] = Multiply" + "\n" +
"[4] = Divide" + "\n");
int c = parseInt(operator);
if (c > 4) {
showMessageDialog(null, "You cant do that.");
} else if (c == 1) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " + " + b + " = " + (a + b));
} else if (c == 2) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " - " + b + " = " + (a - b));
} else if (c == 3) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " * " + b + " = " + (a * b));
} else if (c == 4) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " / " + b + " = " + (a / b));
}
}
}
Try something like
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
switch(c) {
case 1:
showMessageDialog(null, a + " + " + b + " = " + (a+b));
break;
case 2:
...
default:
showMessageDialog(null, "You cant do that.");
Well, to start; you can move the
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
outside of the if blocks so that it only asks once before the if block, which will save you 12 lines of code.
Or you can also use methods or functions as a practice; but that wouldn't shorten your code further, really. I'd also suggest looking into Codegolf, you can learn a LOT about code-shortening.
The following will be identical, but doesn't repeat the same lines over and over. You can also use the switch statement in place of the 4 if/else if statements.
public class SimpleCalc {
public static void main(String[] args) {
String operator = showInputDialog("Choose operation: " + "\n" +
"[1] = Plus" + "\n" +
"[2] = Minus" + "\n" +
"[3] = Multiply" + "\n" +
"[4] = Divide" + "\n");
int c = parseInt(operator);
if (c>4) {
showMessageDialog(null, "You cant do that.");
return;
}
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
if(c==1) {
showMessageDialog(null, a + " + " + b + " = " + (a+b));
}
else if (c==2) {
showMessageDialog(null, a + " - " + b + " = " + (a-b));
}
else if (c==3) {
showMessageDialog(null, a + " * " + b + " = " + (a*b));
}
else if (c==4) {
showMessageDialog(null, a + " / " + b + " = " + (a/b));
}
}
}
There are a couple approaches:
Put the common code into a method
Move the common code to a different part of the current method so that it is executed unconditionally.
Put the non-common code into a function / method / class that can be used to parameterize the common code.
In this case, the second approach works best; e.g.
if(c==1) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " + " + b + " = " + (a+b));
}
else if (c==2) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " - " + b + " = " + (a-b));
}
...
can be transformed into:
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
int result;
char op;
if (c == 1) {
result = a + b;
op = '+';
} else if (c == 2) {
result = a - b;
op = '-';
}
...
showMessageDialog(null, a + " " + op + " " + b + " = " + result);
(I have left a problem there for you to notice and sort out ... as a learning exercise.)
Just for fun. Factor out the stuff that's common! And handle the possibility that you'll need to implement unary operators. You'll probably also want to put it in a loop, and add an exit command.
public class SimpleCalc {
public static void main(String[] args) {
String operator = showInputDialog(
"Choose operation: " + "\n" +
"[1] = Add" + "\n" +
"[2] = Subtract" + "\n" +
"[3] = Multiply" + "\n" +
"[4] = Divide" + "\n");
"[5] = Negate" + "\n");
int c = parseInt(operator);
int operand_count = 0;
switch (c) {
case 1:
case 2:
case 3:
case 4:
operand_count = 2;
break;
case 5:
operand_count = 1;
break;
default:
showMessageDialog(null, "You cant do that.");
return(-1);
}
int a = 0;
int b = 0;
if (operand_count >= 1) {
String textA = showInputDialog("Enter first number: ");
int a = parseInt(textA);
}
if (operand_count >= 2) {
String textB = showInputDialog("Enter second number: ");
int b = parseInt(textB);
}
char * opname = "";
int result = 0;
switch (c) {
case 1:
opname = "+";
result = a + b;
break;
case 2:
opname = "-";
result = a - b;
break;
case 3:
opname = "*";
result = a * b;
break;
case 4:
opname = "/";
result = a / b;
break;
case 5:
opname = "-";
result = -a;
break;
}
if (operand_count == 1) {
showMessageDialog(null, opname + " (" + a + ") = " result);
} else {
showMessageDialog(null, a + " " + opname + " " + b + " = " + result);
}
}
}

Trying to mimic Splitwise app logic - suggest a better way to handle settlements

I am trying to write the basic logic used in apps like Splitwise.
Input - Transactions in the trip
a|a,b,c,d|120
b|a,b,d|210
c|a,b,c,d|40
a|a,b,c|60
a, b, c and d are friends on a trip
in the first transaction a paid 120 units for a transaction involving all four friends
in the second transaction b paid 210 units for a transaction involving only a, b and d
there are n such transactions and there can be m friends
Expected Output
a has to get 50
b has to get 80
c has to give 20
d has to give 110
d has to give 80 to b
d has to give 30 to a
c has to give 20 to a
This is what I tried. Person class is the pojo used.
public class Person {
private String name;
private Integer totalExpense;
private Integer totalSpent;
private Integer balanceAmt;
}
This is the code for settle operation.
public void settle(Map<String, Person> personMap) {
List<Person> getterList = new ArrayList<>();
List<Person> giversList = new ArrayList<>();
for (String key : personMap.keySet()) {
Person user = personMap.get(key);
int balanceAmt = user.getTotalSpent() - user.getTotalExpense();
user.setBalanceAmt(Math.abs(balanceAmt));
if (balanceAmt > 0) {
System.out.println(key + " has to get " + balanceAmt);
getterList.add(user);
} else if (balanceAmt < 0) {
System.out.println(key + " has to give " + Math.abs(balanceAmt));
giversList.add(user);
} else if (balanceAmt == 0) {
System.out.println(key + " is all settled");
}
}
getterList.sort((p2, p1) -> p1.getBalanceAmt().compareTo(p2.getBalanceAmt()));
giversList.sort((p2, p1) -> p1.getBalanceAmt().compareTo(p2.getBalanceAmt()));
giversList.forEach(giver -> {
getterList.forEach(getter -> {
if (getter.getBalanceAmt() == 0) {
return;
}
if (giver.getBalanceAmt() == getter.getBalanceAmt()) {
System.out.println(giver.getName() + " has to give " + giver.getBalanceAmt() + " to " + getter.getName());
giver.setBalanceAmt(0);
getter.setBalanceAmt(0);
} else if (giver.getBalanceAmt() > getter.getBalanceAmt()) {
System.out.println(giver.getName() + " has to give " + getter.getBalanceAmt() + " to " + getter.getName());
giver.setBalanceAmt(giver.getBalanceAmt() - getter.getBalanceAmt());
getter.setBalanceAmt(0);
} else if (giver.getBalanceAmt() < getter.getBalanceAmt()) {
System.out.println(giver.getName() + " has to give " + giver.getBalanceAmt() + " to " + getter.getName());
giver.setBalanceAmt(0);
getter.setBalanceAmt(getter.getBalanceAmt() - giver.getBalanceAmt());
}
});
});
}
The code is not good, it has too many loops.
Suggest a good method to settle the amount and come up with the second part of the output.
Reduce your bottom part's logic from O(m2) to O(m) by using two pointer.
int giverPointer = 0;
int getterPointer = 0;
while(giverPointer < giversList.size() && getterPointer < getterList.size()){
if (giversList.get(giverPointer).getBalanceAmt() == getterList.get(getterPointer).getBalanceAmt()) {
System.out.println(giversList.get(giverPointer).getName() + " has to give " + giversList.get(giverPointer).getBalanceAmt() + " to " + getterList.get(getterPointer).getName());
giversList.get(giverPointer).setBalanceAmt(0);
getterList.get(getterPointer).setBalanceAmt(0);
giverPointer++;
getterPointer++;
} else if (giversList.get(giverPointer).getBalanceAmt() > getterList.get(getterPointer).getBalanceAmt()) {
System.out.println(giversList.get(giverPointer).getName() + " has to give " + getterList.get(getterPointer).getBalanceAmt() + " to " + getterList.get(getterPointer).getName());
giversList.get(giverPointer).setBalanceAmt(giversList.get(giverPointer).getBalanceAmt() - getterList.get(getterPointer).getBalanceAmt());
getterList.get(getterPointer).setBalanceAmt(0);
getterPointer++;
} else {
System.out.println(giversList.get(giverPointer).getName() + " has to give " + giversList.get(giverPointer).getBalanceAmt() + " to " + getterList.get(getterPointer).getName());
giversList.get(giverPointer).setBalanceAmt(0);
getterList.get(getterPointer).setBalanceAmt(getterList.get(getterPointer).getBalanceAmt() - giversList.get(giverPointer).getBalanceAmt());
giverPointer++;
}
}

Output copied twice in Nim game in Java

Im learning java with "programmingbydoing" and i have a problem with Nim game, everything works fine apart from one thing, which is that both:
"System.out.print(n1 + ", choose a pile: ");"
and
"System.out.print(n2 + ", choose a pile: ");"
is out printed twice after the first time.
Here is code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Player one, enter your name: ");
String n1 = input.nextLine();
System.out.print("Player two, enter your name: ");
String n2 = input.nextLine();
int a = 3;
int b = 4;
int c = 5;
int count = 1;
System.out.println("A: "+a+" B: "+b+" C: "+c);
nim_loop:while(a > 0 || b > 0 || c > 0) {
while(count % 2 != 0 ) {
System.out.print(n1+", choose a pile: ");
String first = input.nextLine();
if (first.contains("a") || first.contains("A")) {
System.out.print("How many to remove from pile " + first + "? ");
int second = input.nextInt();
count = count + 1;
a = a - second;
System.out.println("A: " + a + " B: " + b + " C: " + c);
if(a <= 0 && b <= 0 && c <= 0){
break nim_loop;
}
}
else if (first.contains("b") || first.contains("B")) {
System.out.print("How many to remove from pile " + first + "? ");
int second = input.nextInt();
count = count + 1;
b = b - second;
System.out.println("A: " + a + " B: " + b + " C: " + c);
if(a <= 0 && b <= 0 && c <= 0){
break nim_loop;
}
}
else if (first.contains("c") || first.contains("C")) {
System.out.print("How many to remove from pile " + first + "? ");
int second = input.nextInt();
count = count + 1;
c = c - second;
System.out.println("A: " + a + " B: " + b + " C: " + c);
if(a <= 0 && b <= 0 && c <= 0){
break nim_loop;
}
}
}
while(count % 2 == 0) {
System.out.print(n2 + ", choose a pile: ");
String third = input.nextLine();
if (third.contains("a") || third.contains("A")) {
System.out.print("How many to remove from pile " + third + "? ");
int fourth = input.nextInt();
count = count + 1;
a = a - fourth;
System.out.println("A: " + a + " B: " + b + " C: " + c);
} else if (third.contains("b") || third.contains("B")) {
System.out.print("How many to remove from pile " + third + "? ");
int fourth = input.nextInt();
count = count + 1;
b = b - fourth;
System.out.println("A: " + a + " B: " + b + " C: " + c);
} else if (third.contains("c") || third.contains("C")) {
System.out.print("How many to remove from pile " + third + "? ");
int fourth = input.nextInt();
count = count + 1;
c = c - fourth;
System.out.println("A: " + a + " B: " + b + " C: " + c);
}
}
}
if (count % 2 != 0) {
System.out.println("Game ended, Player " + n1 + " is a winner.");
} else if (count % 2 == 0){
System.out.println("Game ended, Player " + n2 + " is a winner.");
}
}
}
And here are the pictures of what happens when i run it:
When the first if condition in first inner loop is true and when you get the user input by using nextInt() it only reads the int value and does not consume the last new line character i,e \n. So the subsequent call to nextLine() will be skipped i,e the nextLine() call in second inner while loop will be skipped without any value but System.out.print(n2 + ", choose a pile: "); will be printed as it is before nextLine() call and control goes back to outer while loop.
Now the count value is 2 so first inner while condition will be false and control goes to second inner while loop. And again it prints b, choose a pile:. Hope this clears your question
Workaround is fire a blank nextLine() call after every nextInt() or use nextLine() inside if condition and parse the user input using Integer.parseInt(String) method.
Example code :
if (first.contains("a") || first.contains("A")) {
System.out.print("How many to remove from pile " + first + "? ");
int second = input.nextInt();
input.nextLine(); // firing an blank nextLine call
count = count + 1;
a = a - second;
System.out.println("A: " + a + " B: " + b + " C: " + c);
if(a <= 0 && b <= 0 && c <= 0){
break nim_loop;
}
For more information - Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Cannot Print Number of Digits Correctly

For my current programming project I am supposed to format my testOne & testTwo like "000". While the average is supposed to be "000.0". I have used DecimalFormat to no avail. Furthermore for my letterGradeArray the letter won't display even though the letter grade is in the actual array. Here is my code:
import java.util.Scanner;
//import java.text.NumberFormat;
import java.text.DecimalFormat;
public class GradeArray
{
#SuppressWarnings({ "unused", "resource" })
public static void main(String[] args)
{
//Setup all the variables
Scanner scan = new Scanner(System.in);
int[] testOne = new int[4]; // Students’ test one grades
int[] testTwo = new int[4]; // Students’ test two grades
double[] average = new double[4]; // Students’ average grades
double z = 002.00;
char letterGrade = 0;
char[] letterGradeArray = new char[4];
DecimalFormat fmt1 = new DecimalFormat("000");
DecimalFormat fmt2 = new DecimalFormat("000.0");
//Begin asking for scores
System.out.println("For test 1,");
for (int i=0;i<testOne.length;i++)
{
System.out.print("Enter score " + (i + 1) + ":");
testOne[i] = scan.nextInt();
fmt1.format(testOne[i]);
}
System.out.println("\nFor test 2,");
for (int i=0;i<testTwo.length;i++)
{
System.out.print("Enter score " + (i + 1) + ":");
testTwo[i] = scan.nextInt();
fmt1.format(testTwo[i]);
}
//Compute average
for(int i=0;i<average.length;i++)
{
{
average[i] = (testOne[i]+testTwo[i])/z;
fmt2.format(average[i]);
}
}
//Compute letter grade
for(int i=0;i<average.length;i++)
{
if (average[i]>= 90 )
{
letterGrade = 'A';
} else if (average[i] >= 80) {
letterGrade = 'B';
} else if (average[i] >= 70) {
letterGrade = 'C';
} else if (average[i] >= 60) {
letterGrade = 'D';
} else {
letterGrade = 'F';
}
//Put the letterGrade into the letterGradeArray
for(int x=0;i<letterGradeArray[x];x++)
{
letterGradeArray[x]=letterGrade;
}
}
//Print it out
System.out.println("Test 1 Test 2 Average Grade");
System.out.println("______ ______ _______ _____");
System.out.println(testOne[0] + " " + testTwo[0] + " " + average[0] +" " + letterGradeArray[0]);
System.out.println(testOne[1] + " " + testTwo[1] + " " + average[1] +" " + letterGradeArray[1]);
System.out.println(testOne[2] + " " + testTwo[2] + " " + average[2] +" " + letterGradeArray[2]);
System.out.println(testOne[3] + " " + testTwo[3] + " " + average[3] +" " + letterGradeArray[3]);
}
}
If anyone has any ideas on how to make this code cleaner do tell me, I feel with all the fors it is clunky.
You are calling fmt1.format(testOne[i]); but you are not doing anything with the result. Calling format returns a String, it does not affect the thing being passed as a parameter, so when you later print testOne[0] etc. you are printing the plain, original, values.
If you want the formatted values you will have to assign the return of .format() to something, keep it around, and print it instead of the original integer values. For example, patterned after how the rest of your code works...
String[] formattedOne = new String[4];
// ... later
formattedOne[i] = fmt1.format(testOne[i]);
// ... still later
System.out.println(formattedOne[0] + " " ..........

Methods/functions in Java

With the given code I was given the directions: Java lets us use Methods/Functions so we can store procedures that we may use more than once, so I would like you to update your code where there is common tasks it can be done inside a method/function. Any idea how to do this?
package fifthAssignment;
public class Arithmetic {
public static void main(String[] args) {
// setting up the variable firstNumber and secondNumber
int length = args.length;
if (length != 3) {
System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
return;
}
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
int addition = firstNumber + secondNumber;
int minus = firstNumber - secondNumber;
int division = firstNumber / secondNumber;
int multiply = firstNumber * secondNumber;
String arithmetic = args[2];
if (arithmetic.equals("+")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + addition);
} else if (arithmetic.equals("-")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + minus);
} else if (arithmetic.equals("/")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + division);
// I could not use "*" operator as it was looking to pull down all
// the files associated with this program instead of
// using it the way I intended to use it. So in this case I changed
// the "*" to "x" so that I can get the solution you
// were looking for.
} else if (arithmetic.equals("x")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + multiply);
}
// following prints out to the console what the length of each argument
// is.
System.out.println(args[0] + " has the length of " + args[0].length());
System.out.println(args[1] + " has the length of " + args[1].length());
if (arithmetic.equals("+")) {
int total = String.valueOf(addition).length();
System.out.println(addition + " has the length of " + total);
}else if (arithmetic.equals("-")) {
int total = String.valueOf(minus).length();
System.out.println(minus + " has the length of " + total);
}else if (arithmetic.equals("/")) {
int total = String.valueOf(division).length();
System.out.println(division + " has the length of " + total);
} else if (arithmetic.equals("x")) {
int total = String.valueOf(multiply).length();
System.out.println(multiply + " has the length of " + total);
}
}
}
I'll provide a singular example, but you should do this on your own.
You have this in your code:
System.out.println(addition + " has the length of " + total);
Instead, you could potentially create a method that would work with two ints:
public void printStatus(int check, int length) {
System.out.println(check + " has the length of " + length);
}
Which would allow you to call
printStatus(addition, total);
This is just a rough example, but you can wrap a "process" of code in a method, and pass the necessary parameters needed to execute the method to it.
package fifthAssignment;
public class Arithmetic {
public static void main(String[] args) {
// setting up the variable firstNumber and secondNumber
int length = args.length;
if (length != 3) {
System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
return;
}
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
int addition = firstNumber + secondNumber;
int minus = firstNumber - secondNumber;
int division = firstNumber / secondNumber;
int multiply = firstNumber * secondNumber;
String arithmetic = args[2];
// following prints out to the console what the length of each argument
// is.
System.out.println(args[0] + " has the length of " + args[0].length());
System.out.println(args[1] + " has the length of " + args[1].length());
performOperation(arithmetic);
}
public void performOperation(String arithmetic) {
if (arithmetic.equals("+")) {
int total = String.valueOf(addition).length();
System.out.println(addition + " has the length of " + total);
} else if (arithmetic.equals("-")) {
int total = String.valueOf(minus).length();
System.out.println(minus + " has the length of " + total);
} else if (arithmetic.equals("/")) {
int total = String.valueOf(division).length();
System.out.println(division + " has the length of " + total);
} else if (arithmetic.equals("x")) {
int total = String.valueOf(multiply).length();
System.out.println(multiply + " has the length of " + total);
}
}
}

Categories

Resources