Reputation: 3
basically, I want to try out the persistence bugger and I'm new to java coding so I'm still using NetBeans GUI to code (I know) but I need help to make it work. Have attached my efforts below. The GUI is just a textField that takes input and a button with the given code. If someone could help that would be great. Thanks in advance.
The error I kept getting was: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 (4 is the length of the number I entered. It kept varying accordingly)
long num1=Long.parseLong(jTextField1.getText());
String num2=jTextField1.getText();
char s1;
int pers=1,temp;
for(int i=1;i<=num2.length();i++)
{
s1=num2.charAt(i);
temp=Character.getNumericValue(s1);
pers*=temp;
if(pers==0)
{
System.out.println(i);
break;
}
}
String num2=jTextField1.getText();
String test=num2;
char s1;
int pers=num2.length(),temp=1,i,n=0;
do
{
for(i=0;i<pers;i++)
{
s1=test.charAt(i);
temp=temp*(Character.getNumericValue(s1));
}
test=Integer.toString(temp);
temp=1;
} while(test.length()>1);
System.out.println(n);
Final working code:
long r1=Long.parseLong(jTextField1.getText());
String s=jTextField1.getText(),s1="";
long r=1;
int i=0;
char[] gfg=s.toCharArray();
do
{
for (char ch : gfg)
{
r *= Character.digit(ch, 10);
}
s1=Long.toString(r);
gfg=s1.toCharArray();
System.out.println(r);
r=1;
}while(s1.length()!=1);
Upvotes: 0
Views: 174
Reputation: 767
A simple way to fix your given error is simple.
Computers count from 0. So calling String.charAt(0)
is calling the first character of the String. Calling array[0]
is calling the first index of the array. Calling String.charAt(1)
or array[1]
is calling the second element.
Your IndexOutOfBoundsException can easily be stopped by changing
for(int i=1; i<=num2.length(); i++){
to for(int i=0; i<num2.length(); i++){
The reason why your code produces an error is:
When the loop starts, your code skips the first character.
In the last iteration of the for-loop where i == num2.length()
causes the problem because you attempt to call the non-existant N+1th character of a string while the length is only N.
The reason why your edited code causes an error is because in the line test=Integer.toString(temp);
, you make test
become a smaller number. (Probably)
Let's say num2
was 989. Test = num2
and pers = num2.length(), which is 3
This is important.
After this line test=Integer.toString(temp);
, test becomes 9
. (As a String)
Remember how test
is only a single length 1
digit? Well, in the next iteration of the do while loop, inside of the for loop, i
to pers
is 0
to 3.
However, again, test
is only a single digit where it's length is 1.
In your for loop try adding System.out.println(test.charAt(i))
.
The most important part of code is the logic behind it. If you lose the code you typed but kept a sheet of planning on how to make it, then you can fabricate the code again. If you forget the logic behind the code, and your teacher asks you to explain it, you're going to start from scratch.
The logic behind your code (as described in the title) to multiply each digit in a number is like this.
Input String
Make COUNT integer, equal to 1
Loop: (Use String like char[] array)
Call String.charAt(int)
Parse String.charAt(int)
COUNT (<<placeholder name) *= parsed integer (valueOf returns the Integer wrapper class, you used int instead)
End loop
Print result
Difference between parseInt and valueOf in java?
Upvotes: 1
Reputation: 201487
Your input is already a String
, convert it to a char[]
and use a for-each
loop. Like,
int r = 1;
for (char ch : jTextField1.getText().toCharArray()) {
r *= Character.digit(ch, 10);
}
Alternatively, valid indices are 0
(inclusive) to length
(exclusive). So your for
loop should look like
int i=1;i<=num2.length();i++
should be
int i=0; i < num2.length(); i++
Based on your comment below that you need to do it twice, I would extract the logic to a method and call it twice. Like,
private static int multiplyDigits(int r) {
int t = 1;
while (r > 0) {
t *= r % 10;
r /= 10;
}
return t;
}
And then
String str = "4544";
System.out.println(multiplyDigits(multiplyDigits(Integer.parseInt(str))));
Upvotes: 0