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 01-09-2008, 03:33 AM
Member
 
Join Date: Dec 2007
Posts: 3
sheetalnri is on a distinguished road
can anyone help me
Hii all...
Hope you all are doing great.
I am actually trying to read an input text file. The file has some points, with each line having only one point. Like:
(1.0,1.0)
(0.0,2.0)
(1.0,2.0)
(1.0,0.0)
and so on.
I want to read and store them one by one, then add one with the another so that I will be able to get an output (3.0,5.0) (which I got by adding all the points).

Now I am able to read the points from a file. But I could not store them add them one with the other. can anyone help me with this issue.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-09-2008, 03:41 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 260
roots is on a distinguished road
For each point use StringTokenizer to get ordinates. Convert to Number using Float.parseFloat method. You can store this number in an ArrayList.

When you are done reading the file. Loop over the array list and add get sum of both the ordinates.
And Bingo !!
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-09-2008, 05:18 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,142
hardwired is on a distinguished road
Code:
import java.io.*; import java.util.*; public class AddingPoints { public static void main(String[] args) { String path = "addingPoints.txt"; Scanner scanner = null; try { scanner = new Scanner(new File(path)); } catch(FileNotFoundException e) { System.out.println("File not found: " + e.getMessage()); } List<Double[]> list = new ArrayList<Double[]>(); while(scanner.hasNextLine()) { String line = scanner.nextLine(); Double[] array = parse(line); System.out.printf("array = %s%n", Arrays.toString(array)); list.add(array); } scanner.close(); System.out.println("------------------"); double[] total = addPoints(list); System.out.printf("total = %s%n", Arrays.toString(total)); } private static Double[] parse(String s) { double[] d = new double[2]; int left = s.indexOf("("); int comma = s.indexOf(","); d[0] = Double.parseDouble(s.substring(left+1, comma)); int right = s.indexOf(")"); d[1] = Double.parseDouble(s.substring(comma+1, right)); return new Double[] { Double.valueOf(d[0]), Double.valueOf(d[1]) }; } private static double[] addPoints(List<Double[]> list) { double x = 0; double y = 0; for(int j = 0; j < list.size(); j++) { Double[] array = (Double[])list.get(j); x += array[0].doubleValue(); y += array[1].doubleValue(); } return new double[] { x, y }; } }
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



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


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