Java cross reference of words in a file?
-
hello, I need to finish a program that will produce a cross reference of the words in a file... the instructions i have been given are below. When I try to run what I have been given the program freezes and nothing happens any help would be too good to be true. Thanks in advance.. Instructions: complete the following program which produces a cross reference of the "words" in a file. A word for this program is something that is surrounded by white space (blanks, tabs, new lines, and/or returns). use your source program itself as input by running the program using the following command at the dos command prompt: java lab10 < lab10.java here is the code I was given: File is named cisio: import java.io.*; public class cisio { static boolean EndOfFile=false; public cisio () { EndOfFile = false; } static int GetChar() { int c; try { c=System.in.read(); if (c == -1) EndOfFile = true; return c; } catch (IOException e) { EndOfFile = true; return -1; } } public static boolean eof() { return EndOfFile; } public static String GetLine() { int i; i= GetChar(); String s=""; while (i>0 && i!= '\n') { if (i !='\r') s= s + (char) i; i = GetChar(); } return s; } public static int GetInt() { return Integer.parseInt(GetLine()); } public static double GetDouble() { return Double.parseDouble(GetLine()); } } File named lab10: class Word { String theWord; String inputLine; int line_number; public Word() { inputLine = ""; line_number = 0; } public String getValue() { return theWord; } public int getNext() { theWord = ""; while (!cisio.eof() && inputLine.length() == 0) { inputLine = cisio.GetLine(); if (cisio.eof()) return 0; line_number += 1; System.out.println(line_number+". "+inputLine); } while ((inputLine.length()>0) && (inputLine.charAt (0) == ' ')) inputLine = inputLine.substring(1); //lines here blank //lines here code //lines here code //lines here code //lines here code //lines here code //System.out.println(theWord); if (cisio.eof()) return 0; return line_number; } } // end class word class WordList { String words []; int numWords; public WordList () { words = new String [1000]; numWords = 0; } public void addWord(Word word, int lineNr) { String t; t = word.getValue()+' '; int i; for (i=0; i<numWords; i++) if((words[i].length()>t.length()) && words [i].substring(0,t.length()).equals (t)) { words[i] = words [i]+lineNr+' '; return; } words[numWords++] = word.getValue()+' '+lineNr+' '; } public void print() { int i; for (i=0; i<numWords; i++) System.out.println(words[i]); } } //end class word list public class lab10 { public static void main (String [] args) { WordList w = new WordList (); Word word = new Word(); int lineNr; lineNr = word.getNext(); while (lineNr !=0) { w.addWord(word,lineNr); lineNr = word.getNext(); } w.print(); } } //end class lab 10
-
Answer:
The part where you need to put in code should read: while ((inputLine.length()>0) && (inputLine.charAt(0) != ' ')) { theWord = theWord + inputLine.charAt(0); inputLine = inputLine.substring(1); } If you mess that up it might freeze, try it.
Jim at Yahoo! Answers Visit the source
Related Q & A:
- How to delete a particular line from a file?Best solution by Stack Overflow
- How to write binary data to a file?Best solution by Stack Overflow
- How can I send a file to a friend on Hotmail?Best solution by askdavetaylor.com
- Do I need a special ISP package to do a file hosting site?Best solution by Yahoo! Answers
- I cannot attach a file to a mail in Yahoo.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.