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 06-09-2008, 07:51 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
How to store 5 numbers from a file into 5 seperate variables?
Ok, I have 5 numbers in a file. Example: 5 8 7 2 1

they are stored in the file just like that with no commas and I need to read that file and store each number into a variable so I can compare each number seperately to another number.

I tried reading the post earlier from someone else but I don't think that this is the same kind of problem.

I could store a seperate file for each number and compare that way, but I am trying to be more efficient and save space. Could someone please tell me how to do this.

Thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-09-2008, 08:24 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Do it with Scanner class....

have some experiments on nextInt() method.....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
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 06-09-2008, 09:21 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Ok, I did a little research on this and I found this code. I modified it a little to work for me, but I know it is not correct, I think that I am closer though, could you please help me understand what I am doing wrong, or need to do next?? thank you


Code:
import java.util.Scanner; import java.io.*; \\this section will be in an event created by a calculate button Scanner scanner = new Scanner(new File("test.txt")); while (scanner.hasNext()) int a = (scanner.next()); int b = (scanner.next()); int c = (scanner.next()); int d = (scanner.next()); int e = (scanner.next());

Is this correct?

I need 5 variables that will store the 5 numbers on the same line in text file so each variable will be available for use later in the code.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-09-2008, 05:41 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Put it on a method instead... and invoke it when some event fired....

Don't forget to use close() method after that process....

Code:
while (scanner.hasNext()) int a = (scanner.next()); int b = (scanner.next()); int c = (scanner.next()); int d = (scanner.next()); int e = (scanner.next());
be careful when to use braces and when not to use it....

that code would only store the data from the file to variable a.

And about that implementation, i think you should initialize those variables on member's field....(global variable)

So that, you can just implement directly setters/getters method...
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by sukatoa : 06-09-2008 at 05:54 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-09-2008, 06:44 PM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
how-store-5-numbers-file-into-5-seperate-variables
Thank you for your help sukatoa

I used the scanner class and nextInt() method like you said, and it almost looked similar to the code I posted earlier but I didn't use the has next, and put nextInt() instead of just next when adding to the variables of the type int.

If I had my code in front, I would have posted it for all to benefit if they are as novice as myself.

Thanks again.
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 would you get information from a file and then store it in an array? szimme101 Advanced Java 3 04-07-2008 08:02 PM
How to store property file into key value pair Java Tip java.util 0 04-05-2008 12:16 PM
store file kazitula Java Applets 0 02-17-2008 11:45 PM


All times are GMT +3. The time now is 02:58 AM.


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