Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 03-31-2008, 06:24 PM
Member
 
Join Date: Mar 2008
Posts: 4
dav9999 is on a distinguished road
[SOLVED] getting values from a text file
I have a great big text file that looks like this:

jackson=4000000
monroe=70000000
lincoln=4777749
etc....

i'm trying to get java to return the value after the = sign.

So if my program asks "Which street value are you interested in?" and let's say they type in jackson, it will return the value 4000000.


I've tried a bunch of different methods but I can't seem to get it to work.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-31-2008, 06:41 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Hello Dav9999 and welcome to the Java Forums.

I suggest using the Java Split function and storing each value into an Array so they can be called later.

Do you understand or do you need help further?
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
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 03-31-2008, 06:43 PM
Member
 
Join Date: Mar 2008
Posts: 4
dav9999 is on a distinguished road
it is a huuuuge text file...
it has about 3000 lines of name=value in it. can i still do that?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-31-2008, 06:45 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
import java.io.*; public class SplittingText { public static void main(String[] args) { String target = "lincoln"; String path = "splittingText.txt"; try { File file = new File(path); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); String line; while((line = br.readLine()) != null) { if(line.indexOf(target) != -1) break; } String[] s = line.split("\\="); System.out.println("value for " + target + " is: " + s[1]); br.close(); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } } }
splittingText.txt
Code:
jackson=4000000 monroe=70000000 lincoln=4777749
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-31-2008, 06:45 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Yeah Java can handle that no problem.

Ah HardWired you love it!!
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
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
  #6 (permalink)  
Old 03-31-2008, 07:07 PM
Member
 
Join Date: Mar 2008
Posts: 4
dav9999 is on a distinguished road
hardwired, thanks I see how that works now, but can the String target be taken from user input?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-31-2008, 07:12 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Yeah Dav it can...

Code:
System.out.println("Which Street?:"); // Opens up standard input BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String target = null; // Read the Street name from the command-line try { target = br.readLine(); } catch (IOException ioe) { System.out.println("IO error reading command line input"); System.exit(1); }
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
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
  #8 (permalink)  
Old 03-31-2008, 07:56 PM
Member
 
Join Date: Mar 2008
Posts: 4
dav9999 is on a distinguished road
how would I include this code in the above?? I tried it like this, but it didn't work

import java.io.*;

public class SplittingText {
public static void main(String[] args) {
System.out.println("Which Street?:");

// Opens up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String target = null;
String path = "splittingText.txt";
try {
target = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error reading command line input");
System.exit(1);
}
String[] s = line.split("\\=");
System.out.println("value for " + target +
" is: " + s[1]);
br.close();
} catch(IOException e) {
System.out.println("read error: " + e.getMessage());
}
}
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-01-2008, 02:51 AM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
this is how.

Code:
import java.io.*; public class SplittingText { public static void main(String[] args) { // initialization BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); String target = null; String path = "splittingText.txt"; try { File file = new File(path); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); // get user input System.out.println("Which street?:"); try { target = bi.readLine(); } catch (IOException ioe) { System.out.println("IO error reading command line input"); System.exit(1); } String line; while((line = br.readLine()) != null) { if(line.indexOf(target) != -1) break; } String[] s = line.split("\\="); System.out.println("value for " + target + " is: " + s[1]); br.close(); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } } }
Also, since you're new, if you were wondering how to get the above box use [/code] and [code] only reversed the other way (Mine are backwards so there isn't a box)

Last edited by Bluefox815 : 04-01-2008 at 02:58 AM.
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
[SOLVED] How to read a file and compare Array values DonCash Advanced Java 2 04-02-2008 03:22 PM
[SOLVED] Reading a text file into an Array DonCash New To Java 6 04-01-2008 01:18 PM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM
Converting text file(.txt) to JPG file(.jpg) in java javadeveloper Advanced Java 0 11-09-2007 05:22 PM
count character in text file as input file aNNuur New To Java 0 06-18-2007 07:46 AM


All times are GMT +3. The time now is 05:05 PM.


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