DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2003
    Posts
    2

    Question convert int to char

    hello everybody!

    i want to alter letters like this:

    char letter;
    int k;
    ...
    letter + k

    for example: a+1=b, b+1=c; x+2=z;...


    i did it like that:

    {
    ...
    String b;
    char a,y;
    int k = 1;
    ...
    b = readWord(); // input is the letter 'A'

    a = b.charAt(0);

    y = a + k;

    print(y);
    ...
    }

    output: 66 ('B' in ASCII) but i want the letter 'B' to be put out. how can i convert the "int" into a "char" again?

    later i want to 'crypt' a whole string.

    thx and nice greez



    ps.: sorry for the sloppy english!

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    It's quite simple, you just do a cast to a char:

    int numericValue = 66;
    char c = (char)numericValue;

    This 'forces' the value into a character variable.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Mar 2003
    Posts
    2
    already tried it like this:

    String b;
    char a,z;
    int x;

    print("String: ");
    b = readWord();

    a = wort.charAt(0);

    x = a;
    z = (char)x;

    println(z);

    doesn't work
    whats wrong?

    thx for helping, greez
    chris

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    I adjusted your code to get it to compile and it behaved as expected, in this case, printing out 'B'.

    Code:
      public class Test {
            public static void main(String[] args) {
               String b;
               char a,z;
               int x;
    
               System.out.println("String: ");
               b = "Beautiful!";
    
               a = b.charAt(0);
    
               x = a;
               z = (char)x;
    
               System.out.println(z);
    
            }
      }
    Run this code. Tell me if this is what you want.

    ArchAngel.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Mar 2003
    Posts
    15
    Hi, out of curiosity I tried to run your code.. with
    import java.swing.*; above the code. And then: 'java Test' at the prompt to create java application.

    The error encountered it says:
    $ /usr/j2se/bin/java test
    Exception in thread "main" java.lang.NoClassDefFoundError: Test

    Wonder what exactly is the problem?

    Is System.exit( 0 ); needed b4 the end of the main to terminate application?

  6. #6
    Join Date
    Mar 2003
    Posts
    15
    I got it! I should have compiled it with javac then run with java Test.

    The output is:
    String
    B

    Sorry.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links