Results 1 to 20 of 41
- 01-21-2009, 02:13 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
Problem with reading text from a .txt file
the program has to read from a file, once it sees ("***") it has to stop and ask to enter any key,once you enter that then the program has to continue doing the work from where it's left
this is what I came up so far:
Main method:
class:Java Code:import java.util.Scanner; import java.io.*; public class Problem2CeasarCipher { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader("DATA21.txt")); Scanner inputs = new Scanner(System.in); char[] wordLetters; int[] letterCounts = new int[26]; int arrayIndex; int offset; int tempIndex = 0; int tempHighest = 0; String sentence = ""; //System.out.println("Enter a sentence: "); while (sentence != null) { if (sentence.equals("***")) { System.out.println("Enter any key to continue: "); int key = inputs.nextInt(); } sentence = input.readLine(); String copySentence = sentence; sentence = sentence.replaceAll(" ", ""); wordLetters = sentence.toCharArray(); for(int letter = 0; letter < wordLetters.length; letter++) { offset = wordLetters[letter] - 65; letterCounts[offset] +=1; } for(int i = 0; i <= 25; i++) { if (letterCounts[i] > tempHighest) { tempHighest = letterCounts[i]; tempIndex = i; } } //System.out.println(alphabet[tempIndex]); testProblem2CeasarCipher encoding = new testProblem2CeasarCipher(tempIndex); // encoding.decode(copySentence); System.out.println(encoding.decode(copySentence)); } } }
file that it suppose to read:Java Code:public class testProblem2CeasarCipher { int e = 4; int shift; int move; public testProblem2CeasarCipher(int mostFrequent) { shift = mostFrequent - e; move = 26 - shift; } public String decode(String word) { StringBuffer result = new StringBuffer(); for (int k = 0; k < word.length(); k++) { char ch = word.charAt(k); if(ch==' ') { ch= ' '; } else { ch = (char)('A' + (ch - 'A' + move) % 26); } result.append(ch); } return (result.toString()); } }
I'm getting NullPointerException, plus it only decrypts the first and the last sentences in the first paragraph and it doesn't ask to enter anything, any help here ? I really need this done, thanksJava Code:LZWJW OSK FGLZAFY KG NWJQ JWESJCSTDW AF LZSL FGJ VAV SDAUW LZAFC AL KG NWJQ EMUZ GML GX LZW OSQ LG ZWSJ LZW JSTTAL KSQ LG ALKWDX GZ VWSJ GZ VWSJ A KZSDD TW DSLW *** NRCQNA CQN FNUU FJB ENAH MNNY XA BQN ONUU ENAH BUXFUH OXA BQN QJM YUNWCH XO CRVN JB BQN FNWC MXFW CX UXXT JKXDC QNA JWM CX FXWMNA FQJC FJB PXRWP CX QJYYNW WNGC ORABC BQN CARNM CX UXXT MXFW JWM VJTN XDC FQJC BQN FJB LXVRWP CX KDC RC FJB CXX MJAT CX BNN JWHCQRWP *** OCZI NCZ GJJFZY VO OCZ NDYZN JA OCZ RZGG VIY IJODXZY OCVO OCZT RZMZ ADGGZY RDOC XPKWJVMYN VIY WJJFNCZGQZN CZMZ VIY OCZMZ NCZ NVR HVKN VIY KDXOPMZN CPIB PKJI KZBN NCZ OJJF YJRI V EVM AMJH JIZ JA OCZ NCZGQZN VN NCZ KVNNZY DO RVN GVWZGGZY JMVIBZ HVMHVGVYZ WPO OJ CZM BMZVO YDNVKKJDIOHZIO DO RVN ZHKOT NCZ YDY IJO GDFZ OJ YMJK OCZ EVM AJM AZVM JA FDGGDIB NJHZWJYT NJ HVIVBZY OJ KPO DO DIOJ JIZ JA OCZ XPKWJVMYN VN NCZ AZGG KVNO DO
-
um,... do you have an actual question?
Often the quality of the answer you receive is directly proportional to the quality of the question asked. Posting code without telling us specifically what's right, what's wrong, what it's not doing, it's doing that it shouldn't be doing, will likely get you lectures like this one. Posting a very specific question will likely get you a very specific answer. Good luck.
- 01-21-2009, 02:18 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
-
Well, then I wish you much luck.
- 01-21-2009, 02:28 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
well, I gave you my code and gave the question, all you have to do is go over it and check what's wrong ?! what else do you expect me to do ? and I already stated my problem
- 01-21-2009, 02:29 AM #6
wow... what a question...
That is one heck of a question. I suggest the following:
- You want quality answers? Ask quality questions.
- To find out what's going on inside your program, you can debug it with printlns. Place a couple of println commands at different parts of the program and you can follow the flow of your program and can pinpoint where the problem is.
- Also, use a smaller set of words/lines/paragraphs
Luck,
CJSL
PS: want more info on debugging?
Debug challenge: how do you debug a Java program?Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-21-2009, 02:30 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
here's what the program has to do:
ecoo.org/ecoocs/boardwide/brd2004p2.html
this is a link to the question, since it would be too long to write it here, well listen, all I'm asking is a little help, (this is New to Java forums after all)
-
Come on now, you're leaving out a lot of important detail. If you get a nullpointerexception, then on what line? The exception should give you the line number and you use that to indicate it to us? Do you get it all the time? what situations do you get it in? Again, details are important.
- 01-21-2009, 02:34 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
there are couple of things YOU can do too to help me out: can you just copy and paste the code in Java program and then try to see what's going on ?
and here's what I'm getting exactly:
Java Code:THERE WAS NOTHING SO VERY REMARKABLE IN THAT YZC OTO LWTNP ESTYV TE DZ GPCJ XFNS ZFE ZQ ESP HLJ EZ SPLC ESP CLMMTE DLJ EZ TEDPWQ OH DEAR OH DEAR I SHALL BE LATE NullPointerException: at Problem2CeasarCipher.main(Problem2CeasarCipher.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
-
Please understand that we are all volunteers here, every one of us. So most of the work debugging your code needs to be done by you. We can help guide you, but still the lion's share of the work most be yours. Makes sense, doesn't it?there are couple of things YOU can do too to help me out: can you just copy and paste the code in Java program and then try to see what's going on ?
So what do you think is happening here? It reads the first four lines fine, but then when it gets the 5th line, it fails. What is on the 5th line of text? Is it regular text? Nope, it's not. So something about your code reading in "*" and trying to decrypt these '*' chars causes your program to go belly up.
So what line is line 37 of your program? I would guess that you need to tell your code there not to analyze the text if it runs into a line of "***" and at least some of your problems will get better.
Best of luck.
-
I like using a Scanner to read my text files, as I find it easier. why not use a Scanner scanner = new Scanner("DATA21.txt")
do
Java Code:while (scanner.hasNextLine()) //then read a line. //then if the line.equals("***") { // get int input } else { //... do everything else }
- 01-21-2009, 02:56 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
-
You don't want it to continue to do anything to that sentence if it runs into "***". You certainly don't want your program to analyze that sentence, for that's why its crashing. You only want to get user input, finish the loop and then loop again getting the next line from the text file and analyzing that. Make sense?
- 01-21-2009, 03:05 AM #14
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
one more, I need the program analyze this:
LZWJW OSK FGLZAFY KG NWJQ JWESJCSTDW AF LZSL
FGJ VAV SDAUW LZAFC AL KG NWJQ EMUZ GML GX LZW OSQ
LG ZWSJ LZW JSTTAL KSQ LG ALKWDX
GZ VWSJ GZ VWSJ A KZSDD TW DSLW
as one sentence, will it do it ?
- 01-21-2009, 03:18 AM #15
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
now this is what I'm getting:
I have to problems, first of all, the program need to recognize those paragraphs as one sentence and second of all, when it sees no more("***") it has to stop, basically when it finishes it has to stop(this is I guess why I'm getting that last error)Java Code:THERE WAS NOTHING SO VERY REMARKABLE IN THAT YZC OTO LWTNP ESTYV TE DZ GPCJ XFNS ZFE ZQ ESP HLJ EZ SPLC ESP CLMMTE DLJ EZ TEDPWQ OH DEAR OH DEAR I SHALL BE LATE Enter any key to continue: [DrJava Input Box] VZKYVI KYV NVCC NRJ MVIP UVVG FI JYV WVCC MVIP JCFNCP FOR SHE HAD PLENTY OF TIME AS SHE WENT DOWN TO LOOK ABOUT HER REU KF NFEUVI NYRK NRJ XFZEX KF YRGGVE EVOK WZIJK JYV KIZVU KF CFFB UFNE REU DRBV FLK NYRK JYV NRJ TFDZEX KF SLK ZK NRJ KFF URIB KF JVV REPKYZEX Enter any key to continue: [DrJava Input Box] FTQZ ETQ XAAWQP MF FTQ EUPQE AR FTQ IQXX MZP ZAFUOQP FTMF FTQK IQDQ RUXXQP IUFT OGBNAMDPE MZP NAAWETQXHQE TQDQ MZP FTQDQ ETQ EMI YMBE MZP BUOFGDQE TGZS GBAZ BQSE ETQ FAAW PAIZ M VMD RDAY AZQ AR FTQ ETQXHQE ME ETQ BMEEQP UF IME XMNQXXQP ADMZSQ YMDYMXMPQ NGF FA TQD SDQMF PUEMBBAUZFYQZF UF IME QYBFK SHE DID NOT LIKE TO DROP THE JAR FOR FEAR OF KILLING SOMEBODY SO MANAGED TO PUT IT INTO ONE OF THE CUPBOARDS AS SHE FELL PAST IT Enter any key to continue: [DrJava Input Box] NullPointerException: at Problem2CeasarCipher.main(Problem2CeasarCipher.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
-
Please repost your latest code.
- 01-21-2009, 03:27 AM #17
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
Java Code:main method: import java.util.Scanner; import java.io.*; public class Problem2CeasarCipher { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader("DATA21.txt")); Scanner inputs = new Scanner(System.in); char[] wordLetters; int[] letterCounts = new int[26]; int arrayIndex; int offset; int tempIndex = 0; int tempHighest = 0; String sentence = ""; //System.out.println("Enter a sentence: "); while (sentence != null) { sentence = input.readLine(); if (sentence.equals("***")) { System.out.println("Enter any key to continue: "); int key = inputs.nextInt(); } else { String copySentence = sentence; sentence = sentence.replaceAll(" ", ""); wordLetters = sentence.toCharArray(); for(int letter = 0; letter < wordLetters.length; letter++) { offset = wordLetters[letter] - 65; letterCounts[offset] +=1; } for(int i = 0; i <= 25; i++) { if (letterCounts[i] > tempHighest) { tempHighest = letterCounts[i]; tempIndex = i; } } testProblem2CeasarCipher encoding = new testProblem2CeasarCipher(tempIndex); System.out.println(encoding.decode(copySentence)); } } } }Java Code:class: public class testProblem2CeasarCipher { int e = 4; int shift; int move; public testProblem2CeasarCipher(int mostFrequent) { shift = mostFrequent - e; move = 26 - shift; } public String decode(String word) { StringBuffer result = new StringBuffer(); for (int k = 0; k < word.length(); k++) { char ch = word.charAt(k); if(ch==' ') { ch= ' '; } else { ch = (char)('A' + (ch - 'A' + move) % 26); } result.append(ch); } return (result.toString()); } }
plus the file:
and it has to look like this:Java Code:LZWJW OSK FGLZAFY KG NWJQ JWESJCSTDW AF LZSL FGJ VAV SDAUW LZAFC AL KG NWJQ EMUZ GML GX LZW OSQ LG ZWSJ LZW JSTTAL KSQ LG ALKWDX GZ VWSJ GZ VWSJ A KZSDD TW DSLW *** NRCQNA CQN FNUU FJB ENAH MNNY XA BQN ONUU ENAH BUXFUH OXA BQN QJM YUNWCH XO CRVN JB BQN FNWC MXFW CX UXXT JKXDC QNA JWM CX FXWMNA FQJC FJB PXRWP CX QJYYNW WNGC ORABC BQN CARNM CX UXXT MXFW JWM VJTN XDC FQJC BQN FJB LXVRWP CX KDC RC FJB CXX MJAT CX BNN JWHCQRWP *** OCZI NCZ GJJFZY VO OCZ NDYZN JA OCZ RZGG VIY IJODXZY OCVO OCZT RZMZ ADGGZY RDOC XPKWJVMYN VIY WJJFNCZGQZN CZMZ VIY OCZMZ NCZ NVR HVKN VIY KDXOPMZN CPIB PKJI KZBN NCZ OJJF YJRI V EVM AMJH JIZ JA OCZ NCZGQZN VN NCZ KVNNZY DO RVN GVWZGGZY JMVIBZ HVMHVGVYZ WPO OJ CZM BMZVO YDNVKKJDIOHZIO DO RVN ZHKOT NCZ YDY IJO GDFZ OJ YMJK OCZ EVM AJM AZVM JA FDGGDIB NJHZWJYT NJ HVIVBZY OJ KPO DO DIOJ JIZ JA OCZ XPKWJVMYN VN NCZ AZGG KVNO DO
Java Code:THERE WAS NOTHING SO VERY REMARKABLE IN THAT NOR DID ALICE THINK IT SO VERY MUCH OUT OF THE WAY TO HEAR THE RABBIT SAY TO ITSELF OH DEAR OH DEAR I SHALL BE LATE press any key to continue.. EITHER THE WELL WAS VERY DEEP OR SHE FELL VERY SLOWLY FOR SHE HAD PLENTY OF TIME AS SHE WENT DOWN TO LOOK ABOUT HER AND TO WONDER WHAT WAS GOING TO HAPPEN NEXT FIRST SHE TRIED TO LOOK DOWN AND MAKE OUT WHAT SHE WAS COMING TO BUT IT WAS TOO DARK TO SEE ANYTHING press any key to continue.. THEN SHE LOOKED AT THE SIDES OF THE WELL AND NOTICED THAT THEY WERE FILLED WITH CUPBOARDS AND BOOKSHELVES HERE AND THERE SHE SAW MAPS AND PICTURES HUNG UPON PEGS SHE TOOK DOWN A JAR FROM ONE OF THE SHELVES AS SHE PASSED IT WAS LABELLED ORANGE MARMALADE BUT TO HER GREAT DISAPPOINTMENT IT WAS EMPTY SHE DID NOT LIKE TO DROP THE JAR FOR FEAR OF KILLING SOMEBODY SO MANAGED TO PUT IT INTO ONE OF THE CUPBOARDS AS SHE FELL PAST IT
Last edited by Gigi; 01-21-2009 at 03:29 AM.
- 01-21-2009, 03:50 AM #18
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
i don't undestand what's the problem, it suppose to stop when it sees a clear line, and how to blend those paragraphs together(because it has to find the most frequently occuring letter in the entire paragraph, considering every single line, not just one of them) and at the same time it should display them in the original form ?
-
Alright, right now your program logic is telling your code to read each line, analyze each line and translate each line, and this is not what you want to do. You need to change it so that it reads in each paragraph and doesn't do the analysis until it is finished reading the paragraph (either it reaches *** or it reaches the end of the text). THEN you analyze the sentence, then you need to translate the stored sentences and decrypt them. I am not going to tell you how to do this as you should think through the logic of what needs to be done and how. it's not hard, but it takes some thinking with a pencil on paper. You need to walk through your code as if it were reading in text and figure out what needs to be changed, thinking as if you were the computer. One thing you'll need to do also is to reinitialize the variables that are used to analyze the text after each session of analysis has been complete. This includes the letterCounts array the tempHighest int and the copySentence String.
So how about give it another go, and see what you can come up with. Oh another thing, try changing this:
to this:Java Code:while (sentence != null) { sentence = input.readLine();
The first one will let a null String slip through and be analyzed while the second will not.Java Code://while (sentence != null) while ((sentence = input.readLine()) != null) { //sentence = input.readLine();
Good luck!Last edited by Fubarable; 01-21-2009 at 04:02 AM.
- 01-21-2009, 04:14 AM #20
Member
- Join Date
- Dec 2008
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Reading Integers from a text file
By tress in forum New To JavaReplies: 6Last Post: 02-26-2011, 05:45 PM -
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
help...! about reading a text file and finding their average
By nemesis in forum New To JavaReplies: 20Last Post: 10-20-2008, 11:02 AM -
Reading two text file and sum them up
By matt_well in forum New To JavaReplies: 36Last Post: 07-22-2008, 02:55 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


LinkBack URL
About LinkBacks


Bookmarks