5 Replies - 3997 Views - Last Post: 10 December 2008 - 10:48 PM Rate Topic: -----

#1 bsimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-July 08

Ordered Pairs in Alogrithm

Posted 10 December 2008 - 06:23 PM

I need to read in data from a file for use in the implementation of the bellman-ford algorithm. I've already got the rest of the code written out but I have been racking my brain trying to figure out the most convenient way to read this information in. The contents of the file would look like this:

5,5
(1,2)(3,4)
(5,-6)(6,7)
etc...

/

I've been trying to use string tokenizer and string builder but I can't seem to get it to work right. Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Ordered Pairs in Alogrithm

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Ordered Pairs in Alogrithm

Posted 10 December 2008 - 06:25 PM

View Postbsimm, on 10 Dec, 2008 - 05:23 PM, said:

I need to read in data from a file for use in the implementation of the bellman-ford algorithm. I've already got the rest of the code written out but I have been racking my brain trying to figure out the most convenient way to read this information in. The contents of the file would look like this:

5,5
(1,2)(3,4)
(5,-6)(6,7)
etc...

/

I've been trying to use string tokenizer and string builder but I can't seem to get it to work right. Any ideas?


Do you need the brackets? If not then you can just import using the .isLetterOrDigit method in the character class? That might work. If you do need the brackets then maybe try using a large character array and then just sort them using a sorting algorithm.(I think that's what you're asking)
Was This Post Helpful? 0
  • +
  • -

#3 bsimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-July 08

Re: Ordered Pairs in Alogrithm

Posted 10 December 2008 - 08:10 PM

I just need the number. The text file actually looks like this:

5 5
(2,6) (4,7)
(3,5) (4,8) (5,-4)
(2,-2)
(3,-3) (5,9)
(1,2) (3,7)



The first number is the total nodes, the second number is the starting node. I was just using nextInt() to get those. I just can't figure out how to read the other values ignoring the parens and commas but keeping the negative sign if present.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Ordered Pairs in Alogrithm

Posted 10 December 2008 - 08:23 PM

I guess the writters of the Java API never though of the bellman-ford algorithm
I think the bes approch wouk be to read a whole line as a String and then to parse it using String.toCharArray() method
Was This Post Helpful? 0
  • +
  • -

#5 JBabineau  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 68
  • Joined: 05-December 08

Re: Ordered Pairs in Alogrithm

Posted 10 December 2008 - 08:41 PM

why not read in the whole line and use Stringtokenizer?
Was This Post Helpful? 0
  • +
  • -

#6 bsimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-July 08

Re: Ordered Pairs in Alogrithm

Posted 10 December 2008 - 10:48 PM

Okay, i've got all the numbers read in but i'm having another problem that seems like it would be simple to fix but I can't figure it out.

In the code i have two ints declared as totalNodes and startingNode. Before I even run the stringtokenizer to take in the numbers, those two ints are initialized as:

totalNodes = input.nextInt();
startingNode = input.nextInt();



This is supposed to take in the first two numbers in the text file I posted above so the stringtokenizer will skip them but I keep getting an InputMismatch error. Thoughts?

This post has been edited by bsimm: 10 December 2008 - 10:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1