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 6 of 6

Thread: Java casting/combining char

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java casting/combining char

    I'm trying to make the program generate a digit from 97-122, then convert the integer into a char then combine all the char together, but I have no idea how to combine all the char together. My code is

         if(UserInput.equals("1"))
         for(int LowerLAmount = 1; LowerLAmount <= passLength;LowerLAmount++)
            {
              int randomLower = (int)((Math.random()*(25))+(97));  
              System.out.println( (char)randomLower);
            }

    So the LowerLAmount is the user input of how many char there should be, for example, if the user input "8" the loop would run 8 time and generate 8 random number from 97-122 ( hence the Math.random), then I want to turn the integer that was generated into char. For example let say the user input "2", the loop when generate 2 number from 97-122. Let say it generate 97 and 98. I would then want to convert the 97 and 98 into char, which would be "a" and "b". Next I want to combine the two together so it print out "ab". Thanks


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Java casting/combining char

    You've posted some I want's but no specific question that I can see. Please let us know exactly where you're stuck.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java casting/combining char

    Oh sorry, I wanted to explain it throughly and say where I'm stuck at, so I wrote that long paragraph at the bottom lol. But I'm stuck at trying to combine "char" together. Kind of hard to explain without saying what I wrote in that last paragraph again. Sorry

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Java casting/combining char

    Ah, I see. You can create a String *before* your loop, and then inside of the loop, concatenate the char onto the String using the += operator.

    Note that a StringBuilder will do this more efficiently than a String, but your purposes, a String will work adequately.

    Note that you are using "magic" numbers, 25, 97, 122, and this can create code that is hard to debug. You should try to avoid doing this and instead use variables that make sense. Here 'a' represents 97 and 'z' 122, and so the code would make more sense using these chars:

    public class Test {
       public static void main(String[] args) {
          int min = 'a';
          int max = 'z';
          for (int i = min; i <= max; i++) {
             System.out.printf("%03d: %c%n", i, i);
          }
     
          System.out.println();
          for (int i = 0; i < 200; i++) {
             char randomChar = (char)(Math.random() * ('z' - 'a' + 1) + 'a');
             System.out.println(String.valueOf(randomChar));
          }
       }
    }

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java casting/combining char

    I try to create the String before the loop but it not working. Here is what I did

    String LowerAmount="";
         if(UserInput.equals("1"))
         for(int LowerLAmount = 1; LowerLAmount <= passLength;LowerLAmount++)
            {
              int randomLower = (int)((Math.random()*(25))+(97));
              LowerAmount += (char)(randomLower);
     
            }
            System.out.println (LowerAmount);

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Java casting/combining char

    Quote Originally Posted by maple1100 View Post
    I try to create the String before the loop but it not working. Here is what I did...
    "not working" doesn't give us enough information to allow us to help. How is it not working? What is it not doing that it should be doing? What is it doing that it shouldn't be doing?

    And again, avoid "magic" numbers.

    --- Update ---

    Edit: you're changing your loop index inside the loop -- don't do that.

Similar Threads

  1. Casting java.lang.String to a custom class
    By Tyluur in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 29th, 2012, 09:51 PM
  2. HELP: I have problem casting from Vector to Integer in Java
    By tintin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2011, 12:39 PM
  3. [SOLVED] Java Reflection, laoding external classes and casting
    By ashenwolf in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2011, 01:33 PM
  4. Combining Shapes + Text
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: February 17th, 2011, 10:15 AM
  5. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 05:35 PM