Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-07-2008, 08:03 PM
Member
 
Join Date: Sep 2008
Posts: 14
2potatocakes is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-07-2008, 08:10 PM
Senior Member
 
Join Date: Aug 2008
Posts: 178
Supamagier is on a distinguished road
Do you know how to read the file? if so, just use
Code:
String.toCharArray
if not, google it. no spoon feeding.
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-07-2008, 09:32 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-07-2008, 11:14 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-08-2008, 09:08 AM
Member
 
Join Date: Feb 2008
Posts: 22
pawankumarom is on a distinguished road
Char var
I also want to know how many type very able in java.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-08-2008, 09:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-11-2008, 01:20 AM
Member
 
Join Date: Sep 2008
Posts: 14
2potatocakes is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-11-2008, 02:04 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-11-2008, 02:28 AM
Member
 
Join Date: Sep 2008
Posts: 14
2potatocakes is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-11-2008, 04:51 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you read from a file, and then store the info in an array? szimme101 New To Java 5 07-30-2008 11:30 AM
How to convert List to Array Java Tip java.lang 0 04-17-2008 12:37 AM
Traversing through a stack of objects, and puttin them info in an array szimme101 New To Java 1 03-25-2008 07:06 AM
Cannot convert from char to String error sondratheloser New To Java 1 12-13-2007 11:28 PM
Convert a vector to a string array orchid New To Java 2 05-06-2007 09:14 AM


All times are GMT +3. The time now is 01:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org