hello, this program works, but the problem is that I don't know how to format the centre part of the multiplication. how do you read a value from a 3 digit integer? I used IndexOf() and it keeps giving me a -1. for example, if int num = 637 intnum.indexOf(2) gives me a -1
i want to get the value of "7" from position 2, how do i do that?? PLease help!!
import javax.swing.*; // import libraries
public class Assignment1b { public static void main (String [] args) throws Exception { JOptionPane.showMessageDialog (null, "This program multiplies two 3 digit numbers by hand."); // welcome screen output String strFirstNum = JOptionPane.showInputDialog ("Enter the first number to be multiplied: "); // prompts user input int intFirstNum = Integer.parseInt (strFirstNum); // convert string to integer
while (intFirstNum <= 99 || intFirstNum >= 1000) // check valid input of first number { strFirstNum = JOptionPane.showInputDialog ("Number must be 3 digits, re-enter the number:"); intFirstNum = Integer.parseInt (strFirstNum); }
if (intFirstNum >= 99 || intFirstNum <= 1000) { String strSecondNum = JOptionPane.showInputDialog ("Enter another number:"); //prompts for second number int intSecondNum = Integer.parseInt (strSecondNum);
while (intSecondNum <= 99 || intSecondNum >= 1000) // check valid input of second number { strSecondNum = JOptionPane.showInputDialog ("Number must be 3 digits, re-enter the number:"); intSecondNum = Integer.parseInt (strSecondNum); }
if (intSecondNum >= 99 || intSecondNum <= 1000) // output result { System.out.println ("The answer is: \n"); System.out.println (" " + intFirstNum); System.out.println ("x" + " " + intSecondNum); System.out.println ("-------");
int i = strSecondNum.indexOf(2); String k = String.valueOf(intSecondNum.indexOf(2));
System.out.println (k);
System.out.println ("-------"); System.out.println (" " + intFirstNum * intSecondNum); } } // end if
I'm assuming you are talking about String.indexOf(), even though I can't see where you declared "intnum." indexOf(2) will look in the String for a character that has the integer value of 2 and return its index if it finds it, otherwise it will return -1. Since 2 isn't even a valid character value, you will usually get -1. Even indexOf((int)'2') wouldn't work, since the string "637" doesn't include a '2' character.
You probably want to use String.charAt() or String.substring() to get the character at a particular location. Then you should be aware that there is a difference between the ascii representation of a digit and its numerical value. If you are working from characters, then you can subtract '0' and cast to int. If you are using a string, you can use parseInt().
The other way to do this is get the whole integer value with parseInt(), then use division and modulo with to ten to get the digit you desire. For example:
class Ten
{
publicstaticvoid main(String [] args)
{
int x = 1234;
if( args.length != 0 )
x = Integer.parseInt(args[0]);
System.out.println( "The tens digit of " + x + " is " + x / 10 % 10 );
}
}
hmm, i finally got it to work by combining both of what u gave me:
String strValue2 = strSecondNum.substring(2); // take value at position 2 int intValue2 = Integer.parseInt (strValue2); // convert string value to int value
String strValue1 = strSecondNum.substring(1); // take value at position 2 int intValue1 = Integer.parseInt (strValue1);
String strValue0 = strSecondNum.substring(0); // take value at position 2 int intValue0 = Integer.parseInt (strValue0);
System.out.println (" " + (intValue2*intFirstNum)); System.out.println (" " + ((intValue1/ 10 % 10)*intFirstNum)); // use division and modulus to get value at tenths position System.out.println (" " + ((intValue0/ 100 % 10)*intFirstNum)); // use division and moduluts to get value at hundreths position
however, i cant get the spaces to format properly for any case....
Here you would quickly see that there are two flavors of substring(), one that takes a beginIndex parameter and one that takes a beginIndex and an endIndex parameter.