The Artima Developer Community
Sponsored Link

Java Answers Forum
Can someone help me with this Java program please!!!!!!!

6 replies on 1 page. Most recent reply: Jul 5, 2002 5:33 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Can someone help me with this Java program please!!!!!!! Posted: Jul 4, 2002 11:50 PM
Reply to this message Reply
Advertisement
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

} // main method
} // Assignment1b class


abhijeet

Posts: 21
Nickname: sony
Registered: May, 2002

Re: Can someone help me with this Java program please!!!!!!! Posted: Jul 5, 2002 12:29 AM
Reply to this message Reply
Hi,

You have to use substring(int) method:

For Example:-

String testString = "637";
String strValue = testString.substring(2);

This will output "7" when you print "strValue".

abhijeet.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Can someone help me with this Java program please!!!!!!! Posted: Jul 5, 2002 12:48 AM
Reply to this message Reply
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
{
   public static void 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 );
   }
}

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Can someone help me with this Java program please!!!!!!! Posted: Jul 5, 2002 1:18 PM
Reply to this message Reply
Hey I got it!! thanks alot!!!

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Can someone help me with this Java program please!!!!!!! Posted: Jul 5, 2002 1:26 PM
Reply to this message Reply
abhijeet,

when i tried using your substring method,it worked only for the (2) position
but when i tried:

String testString = "637";
String strValue = testString.substring(1);

it didnt give 3, but it gave 37??

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Can someone help me with this Java program please!!!!!!! Posted: Jul 5, 2002 1:43 PM
Reply to this message Reply
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....

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Can someone help me with this Java program please? Posted: Jul 5, 2002 5:33 PM
Reply to this message Reply
Sounds like what you needs it the Java API documentation online at http://java.sun.com/j2se/1.4/docs/api/index.html and available for download at: http://java.sun.com/j2se/1.4/download.html#docs

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.

Flat View: This topic has 6 replies on 1 page
Topic: copy/cut/paste under macos AND windows. Previous Topic   Next Topic Topic: TCP/IP in Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use