Results 1 to 4 of 4
  1. #1
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default Random lowercase letter

    I need to generate a random lowercase letters to be written to file.

    Here is the code I have so far:

    Java Code:
    public static void main(String[] args) throws Exception {
          
          Random r = new Random();
          
          // Write data to file.      
          PrintWriter data = new PrintWriter("Ex19.txt");
          for (int i = 0; i < 50; i++)
             data.print((char)((r.nextInt(26) + 'a')) + " ");
          data.close();      
    }
    This code is writing weird characters to file. E.g.:

    ⁶⁺⁵⁳⁰⁦⁩⁰⁣⁲⁨⁷⁢⁰⁸⁦⁢⁹⁷⁩⁶⁷⁢⁡⁹⁴⁶⁤⁺⁺⁢⁹⁧⁴ⁱ⁢⁤⁡⁨⁳ ⁦⁳⁡ⁱ⁡

    What is throwing me off is that changing the code to
    Java Code:
    data.print((r.nextInt(26) + 'a') + " ");
    appears to write the correct range of numbers to file (i.e. between 97 & 122).

    Can someone please help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't know. This code seems to work OK for me:

    Java Code:
          Random r = new Random();
    
          // Write data to file.
          // PrintWriter data = new PrintWriter("Ex19.txt");
          for (int i = 0; i < 50; i++) {
             System.out.print((char) ((r.nextInt(26) + 'a')) + " ");
          }
          System.out.println();
    edit:
    and then when I tried your code with the PrintWriter, I got this as an output:
    o q c v k r a h s o r v k z z k y j h l a r t w h f i z f d h b d q f i c o b w t y z n g i j u c t

    So it looks good to me! Are you sure you're looking in the correct file?
    Last edited by Fubarable; 05-30-2011 at 03:59 PM.

  3. #3
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    After reading this text file back into the program and getting legitimate characters, I opened the text file using WordPad (instead of Notepad) and the characters are ok!

    Must be some weird Windows thing....


  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. [Q] Generate Random Letter
    By iriscience in forum New To Java
    Replies: 11
    Last Post: 01-31-2011, 01:10 AM
  2. Check uppercase or lowercase letter
    By ala_mages in forum New To Java
    Replies: 2
    Last Post: 12-30-2010, 03:16 PM
  3. ArrayList<String> convert to lowercase
    By s_mehdi76 in forum New To Java
    Replies: 3
    Last Post: 10-30-2010, 06:33 PM
  4. uppercase and lowercase please help
    By cuziwow in forum New To Java
    Replies: 4
    Last Post: 04-11-2010, 08:54 AM
  5. String uppercase/lowercase
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-19-2007, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •