5 Replies - 733 Views - Last Post: 21 November 2012 - 02:20 PM Rate Topic: -----

#1 GDubz   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 06-January 12

error reading file to map

Posted 21 November 2012 - 06:27 AM

Im trying to read a file, set up like this:

Term
Definition

Example:

meaning
something that one wishes to convey, especially by language

term
a word whose definition is in a glossary

word
a string of characters in a language, which has at least one character

definition
a sequence of words that gives meaning to a term

glossary
a list of difficult or specialized terms, with their definitions,
usually near the end of a book

This code reads the term and definition to a map with the word as the key.

Here's what ive got:

private static void addToMap(String fileName, Map<String, String> words) {

		SimpleReader inFile = new SimpleReader1L(fileName);

		while (!inFile.atEOS()) {
			String word = inFile.nextLine();
			String def = inFile.nextLine();
			while (!inFile.nextLine().equals("")) {
				def += inFile.nextLine();
			}
			words.add(word, def);
		}

		System.out.println(words);
	}


It is currently skipping every other item and giving, this result:

{(glossary,a list of difficult or specialized terms, with their definitions,a set of strings of characters, each of which has meaning),(definition,a sequence of words that gives meaning to a term),(term,a word whose definition is in a glossary),(book,a printed or written literary work),(word,a string of characters in a language, which has at least one character),(meaning,something that one wishes to convey, especially by language)}

Any help with reading files like this would also be very helpful, thanks guys

This post has been edited by GDubz: 21 November 2012 - 06:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: error reading file to map

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: error reading file to map

Posted 21 November 2012 - 06:36 AM

Use generic api classes for your IO - Scanner in this case. The code will be pretty much like yours:

while(s.hasNextLine()) {
   words.put(s.nextLine(), s.nextLine());
}

This post has been edited by g00se: 21 November 2012 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#3 GDubz   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 06-January 12

Re: error reading file to map

Posted 21 November 2012 - 12:42 PM

that wont work when the definitions are longer than 2 lines, and java generic classes wont work this
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: error reading file to map

Posted 21 November 2012 - 01:03 PM

Sorry. Just noticed each entry is delimited by a blank line. Accumulate the definition in a StringBuilder
Was This Post Helpful? 1
  • +
  • -

#5 GDubz   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 06-January 12

Re: error reading file to map

Posted 21 November 2012 - 01:19 PM

Thanks for your help, I figured it out
while (!inFile.atEOS()) {
			String word = inFile.nextLine();
			String def = inFile.nextLine();
			String result = inFile.nextLine();
			while(!result.equals("")){
				def += result;
				result = inFile.nextLine();
			}
			words.add(word, def);
		}

This post has been edited by GDubz: 21 November 2012 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: error reading file to map

Posted 21 November 2012 - 02:20 PM

What you need to avoid is this kind of Map entry

{ cat, an animal blah blah blah withfour legs }
lacking proper spacing
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1