Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: String to Int

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String to Int

       import java.io.*;
       import java.net.*;
       import java.util.*;
       import java.lang.*;
       import javax.swing.JOptionPane;
       import java.text.DecimalFormat;
       import java.text.NumberFormat;
       import java.math.*;
     
       public class EchoClient
       {
          public static void main(String[] args)
          {
          // declare objects we require
             Socket  localSocket;
             BufferedReader in;
             BufferedReader kbdInput;
             PrintWriter out;
             String hostname = new String();
             String s, s1;
     
             BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
     
     
     
             System.out.println("Server IP Address : ");
             try
             {
                hostname = input.readLine( );
             }
                catch(Exception e)
                {
                   System.out.println("Exception: " + e.getMessage( ) + "has occurred");
                }
     
     
             try
             {
             // create a socket connected to remote Port 9999; DNS lookup is automatic
                localSocket = new Socket (hostname, 9999);
     
                      // acknowledgement of connection request
                System.out.println(" Connection made with " + localSocket.getInetAddress( ) +
                      "\nRemote Port: " + localSocket.getPort( )+
                      "\nLocal Port: " + localSocket.getLocalPort( ));
     
             // set up data streams in and out of socket and from keyboard
                in = new BufferedReader(new InputStreamReader(localSocket.getInputStream( )));
                out = new PrintWriter(localSocket.getOutputStream( ));
                kbdInput = new BufferedReader(new InputStreamReader(System.in));
     
             // Lab 2 - (2)(a) Bob uses PublicAlice key to encrypt a plaintext message M
     
                BigInteger n,e, p,q,d, exponent;
     
                s1 = in.readLine( );
                System.out.println("Value of n: "+s1);
                n = new BigInteger(s1);
     
                s1 = in.readLine( );
                System.out.println("Value of e: "+s1);
                e = new BigInteger(s1);
     
     
             // while we have a connection
                while (true)
                {
                   BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
                // read keyboard to String
                   //s = kbdInput.readLine( );
                // Lab 2 - (2)(b) Bob uses PublicAlice key to encrypt a plaintext message M
                   char M;
     
                               System.out.println("Enter value of M: ");
                                 String word = buff.readLine(); 
     
     
                               int j = Integer.valueOf(word).intValue();
                   BigInteger b1 = BigInteger.valueOf(j);                     
                   BigInteger num = b1.modPow(e,n);
                   NumberFormat matter = new DecimalFormat("#0");
                                 System.out.println(matter.format(num));
     
     
     
                   out.println(num);
                   out.flush();   
                   //}         	
                   System.out.println(in.readLine( ));
                }
             }
                catch (UnknownHostException e)
                {
                   System.out.println(e.getMessage( ));
                }
                catch (IOException e)
                {
                   System.out.println(e.getMessage( ));
                }
          }
     
       }
    System.out.println("Enter value of M: ");
    String word = buff.readLine();


    int j = Integer.valueOf(word).intValue();

    i am try to convter a string, like eg: HI into int. However it give me error and how to do anyway?


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: String to Int

    The Integer.valueOf method expects a String representation of a number, e.g. "45". It doesn't know the int value of "HI", neither do I. What is the int value you think "HI" should have?

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String to Int

    the value of HI is 72, 73 because H in ascii is 72 ans I is 73. But now i want HI together is it need to (72*73)=HI?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: String to Int

    Hint:
    System.out.println((int)'a');

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: String to Int

    Quote Originally Posted by Joy123 View Post
    the value of HI is 72, 73 because H in ascii is 72 ans I is 73. But now i want HI together is it need to (72*73)=HI?
    '72, 73' is not an int. The compiler is not a mind-reader - you can get the UNICODE value of each character (equivalent to ASCII for the latin alphabet) by casting to an int as Junky shows above. What you do with the two character values is up to you.

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String to Int

    How to do? Can show example?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String to Int

    If you want to convert some int values to single character Strings and then concatenate them.
    If the int values can be cast to char, the String class has a constructor to create a String.
    Then use + to concatenate the Strings.

    Can you give some examples of a String and the int value that you want to convert it to?

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: String to Int

    Quote Originally Posted by Joy123 View Post

    i am try to convter a string, like eg: HI into int. However it give me error and how to do anyway?
    Pardon me if this is a noob answer....

    But have you tried using parseInt?

    i.e:
    String HI = whatever it equals
    int hi = Integer.parseInt(HI);

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String to Int

    I read the question as: How to convert the String "HI" to an int value.
    Try this: System.out.println("HI=" + Integer.parseInt("HI", 26)); // HI=460

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String to Int

    Anyway Thank for tips and guide

  11. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: String to Int

    Quote Originally Posted by Norm View Post
    I read the question as: How to convert the String "HI" to an int value.
    Try this: System.out.println("HI=" + Integer.parseInt("HI", 26)); // HI=460
    nice one - base 26 does have some uses after all!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String to Int

    What is the word for that base? Hexadecimal for 16. My latin is too old.

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: String to Int

    Quote Originally Posted by Norm View Post
    What is the word for that base? Hexadecimal for 16. My latin is too old.
    Hexavigesimal - Wikipedia, the free encyclopedia
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String to Int

    public class Test{
        public static void main(String[] args){
            String HI = "HI";
            String s = "";
            for (int i=0;i<HI.length();i++){
                char a = HI.charAt(i);
                int j = a;
                s=s+j;
            }
            System.out.println(s);
        }
    }
    Last edited by navis; June 22nd, 2011 at 05:49 AM.

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: String to Int

    navis- why have you resurrected an old post? Why have you attempted to spoon-feed the OP an answer instead of offering hints? I'm pretty sure that your attempt is incorrect anyway, but since the OP never truly specified what the requirements were, we can't tell.

    Either way, I don't really see the point of your post. I appreciate you trying to help, but spoon-feeding is NOT helping, and resurrecting old threads is pretty fruitless.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String to Int

    My apologies KW. I got the point.

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: String to Int

    Quote Originally Posted by navis View Post
    My apologies KW. I got the point.
    It's okay. Helping out is great, but spoonfeeding answers is not great, and resurrecting old posts usually is a waste of time. Welcome to the forums.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM