Results 1 to 10 of 10
  1. #1
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default Anyone know how to take info from a txt file and convert it to a char array?

    I'm really sorry guys, In my head this seems like the easiest thing in the world to do, but I cannot get it to work?

    Can anyone suggest how?

    me = noob

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Do you know how to read the file? if so, just use
    Java Code:
    String.toCharArray
    if not, google it. ;) no spoon feeding.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    Do you want one entry in the array for each character in the file?
    What character set is the file in? ASCII or ? ASCII would be the easiest.
    Look thru the API doc I/O packages to find an input method that will return a single character. Perhaps some version of read(). Then open the file, read it a character at a time and save the char in the array. Some input methods will "eat" lineend characters and you won't see them. Is that important?

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default spoonfeeder for hire,....

    I will gladly spoon feed this:

    Use any Reader subclass that has a readLine();

    That goes to a String, which is subject to all manner of sane approaches. You just do it one line at a time.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    pawankumarom is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Char var

    I also want to know how many type very able in java.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pawankumarom View Post
    I also want to know how many type very able in java.
    Are you talking about variable types? If so, better to read more about Java basis.

  7. #7
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default sorry, I was being a little too basic huh..?

    Thanks to all the guys that wrote something constructive on here ;)

    But it was my fault for not asking the question correctly. It was more like half of a question. Here's my dilemna:

    I have a txt file filled with hundreds and thousands of digits and no spaces.

    I can read from the text file no probs and have been using get chars to select the digits I need. But I can't figure out how to convert the newly formed character string into an integer. It won't accept Integer.parseInt ..well it compiles ok, but always throws up an error when it runs.

    Am I doing something blatantly obvious or is their a trick to converting either a char array or a string from a text file into an integer?

    I'm just using the following lines to test if it works.

    String nmbrs = contents.toString();
    System.out.println(nmbrs);

    // char[] textArray = new char[3];
    // nmbrs.getChars(9, 12, textArray, 0);
    // System.out.println(textArray);

    int difx;
    difx=Integer.parseInt(nmbrs);
    System.out.println(difx+1);

    Can anyone see what I'm doing wrong?

    Again, thanks for the help guys ;)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    always throws up an error when it runs.
    Copy and paste the full text of the error message.
    Show the output from your program. You have println()s, what do they show?
    Also show some of the data file that you are reading.

  9. #9
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default no probs, here's the source and results

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    public class ReadTextFile
    {
    public static void main(String[] args)
    {
    File file = new File("c:\\myjava\\test.txt");
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    try
    {
    reader = new BufferedReader(new FileReader(file));
    String text = null;

    while ((text = reader.readLine()) != null)
    {
    contents.append(text)
    .append(System.getProperty("line.separator"));
    }
    } catch (FileNotFoundException e)
    {
    e.printStackTrace();
    } catch (IOException e)
    {
    e.printStackTrace();
    } finally
    {
    try
    {
    if (reader != null)
    {
    reader.close();
    }
    } catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    String text = contents.toString();
    System.out.println(text);

    char[] textArray = new char[3];
    text.getChars(9, 12, textArray, 0);
    System.out.println(textArray);

    int difx;
    difx=Integer.parseInt(text);
    System.out.println(difx+1);
    }
    }

    And the error outputs:

    The current directory is: @MyProjects
    61541116546878646541321357687461351354638541351864 1 <-- this is the full number in my text file

    468 <-- the getChars numbers

    Exception in thread "main" java.lang.NumberFormatException: For input string: "6154111654687864654132135768746135135463854135186 41
    "
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:459)
    at java.lang.Integer.parseInt(Integer.java:497)
    at ReadTextFile.main(ReadTextFile.java:57)
    Interactive Session Ended

    What do you think?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    How big an integer value do you want to get? 2G is max
    How many characters are you trying to convert to an int?
    It looks like too many. Look at the BigInteger class.

    Comment: add comments to the println() to id what is being output. For example:
    System.out.println("text=" + text + "<");
    I can't easily tell what your program has printed and what you have typed in. I need to see what the program is outputing, EXACTLY.

Similar Threads

  1. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 02:31 AM
  2. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  3. How to convert List to Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  4. Replies: 1
    Last Post: 03-25-2008, 05:06 AM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 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
  •