Jump to content

Java Scanner Reader

- - - - -

  • Please log in to reply
1 reply to this topic

#1
mjbourquin

mjbourquin

    Newbie

  • Members
  • Pip
  • 1 posts
I think I have this written correctly but I need to know for sure if this code will parse the text fill below correctly all the way to the end?

import java.util.Scanner;

import java.lang.String;

public class DataParser

{

	private Scanner source;

	public DataParser()

	{

		Scanner source = new Scanner(PA1-exampleData.txt);

		String Member = Scanner.nextLine();

		String[] result = Member.split();

		Member Member = new Member(result[1], result[2], result[3]);

		for(String cur = Scanner.nextLine(); Scanner.hasNextLine()!=false; cur = Scanner.nextLine())

		{

			cur = Scanner.nextLine();

			result = cur.split();

			Item item = new Item(result[1], result[2], result[3], result[4], result[5]);

			cur = Scanner.nextLine();

			result = cur.split();

			Item reference = new Item(result[1], result[2], result[3], result[4], result[5]);

			Edge edge = new Edge(item, reference);

			Member.getEdgeStack.push(edge);

		}

	}

}

PA1-exampleData.txt

member mj Michael Bgg
edge
item 1 comment mj hello 1
reference [itemID] [type] [creator] [msg] [age]
edge
item [itemID] [type] [creator] [msg] [age]
reference [itemID] [type] [creator] [msg] [age]
edge
item [itemID] [type] [creator] [msg] [age]
reference [itemID] [type] [creator] [msg] [age]

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

I need to know for sure if this code will parse the text fill below correctly
Have you tried compiling and executing the code? That is what anyone would have to do to see what the code does.

Do you have any debugging print outs that show if the reading and parsing was successful?
If not, you need to add some so you can tell if the program is doing what you want it to do.

One problem I see is that the code doesn't do any checking to make sure the array create by split has the correct number of elements.
Another problem is that array indexing starts at 0 not 1.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users