Results 1 to 3 of 3
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default reading input from text and convert it to character array?

    Assume that there is a file named abc which contains of characters a b c d e ... inside

    I need to read from the file and convert it to character array. How to do it?
    I have tried several ways but it failed to do it

    Java Code:
     Scanner input = new Scanner("abc.txt");
           while(input.hasNext()){
               char s = input.next().charAt(0);
           }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: reading input from text and convert it to character array?

    The String class has a toCharArray() method (read its API documentation) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: reading input from text and convert it to character array?

    Do you mean to do it in this way?
    Java Code:
     while(input.hasNext()){
               String str = input.nextLine();
               char[] chars = str.toCharArray();
           }
    But how to call back the character array outside of the while loop?

Similar Threads

  1. Please help reading input from text file...
    By kkmoslehpour in forum New To Java
    Replies: 11
    Last Post: 10-26-2012, 03:50 AM
  2. How to convert array list into character
    By suniva in forum New To Java
    Replies: 1
    Last Post: 07-28-2012, 03:49 PM
  3. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  4. Replies: 2
    Last Post: 08-21-2010, 10:53 AM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM

Posting Permissions

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