Results 1 to 20 of 25
Thread: Code Detection
- 01-22-2012, 03:37 PM #1
Code Detection
Guys I need help... do somebody knows how to verify the input file "SecretCode.txt"? this is my code:
I really badly want to know how to verify this kind of code...Java Code:import java.io.*; import java.util.*; public class CodeDetection { static final int maxCodeSize = 250; public static void main (String[] args) throws FileNotFoundException { int[] codeArray = new int[maxCodeSize]; int codeLength; Scanner codeFile = new Scanner(new FileReader("SecretCode.txt")); PrintWriter outFile = new PrintWriter("SecretCodeOut.txt"); // Read the first number (for the number of digits to follow). codeLength = codeFile.nextInt(); if (codeLength <= maxCodeSize) { // Call the readCode() method to read the first half. // The codeArray is loaded with the digits of the secret code. readCode(codeFile, codeArray, codeLength); // Call the compareCode() method to read the second half and // compare with the first half stored in the coreArray. compareCode(codeFile, outFile, codeArray, codeLength); } else System.out.println("Length of the secret code must be <= " + maxCodeSize); codeFile.close(); outFile.close(); } public static void readCode(Scanner inFile, int[] list, int length) { for (int count = 0; count < length; count++) list[count] = inFile.nextInt(); } public static void compareCode(Scanner inFile, PrintWriter outP, int[] list, int length) { int length2, digit; boolean codeOk; codeOk = true; // initialize the flag to true length2 = inFile.nextInt(); // read the first number in the 2nd half if (length != length2) { System.out.println("The original code and its copy are not of " + "the same length."); return; } // First write the header line to the output file. outP.println("Code Digit Code Digit" + "Copy"); // Read that many digits from the input file. for (int count = 0; count < length; count++) { digit = inFile.nextInt(); // Write the digit from the array and one which is just read. outP.printf("%5d %15d", list[count], digit); if (digit != list[count]) // If they don't match.. { outP.println(" corresponding code digits not the same"); codeOk = false; } else outP.println(); } if (codeOk) outP.println("Message transmitted OK."); else outP.println("Error in transmission. Retransmit!!"); } } ================================================================= Input file SecretCode.txt Code Digit Code Digit Copy 9 9 2 2 7 7 8 8 3 3 5 5 6 6 Message transmitted OK.
Last edited by Sakura Lime; 01-23-2012 at 07:34 AM. Reason: Added [code] ... [/code] tags
- 01-22-2012, 04:36 PM #2
Re: Code Detection
Please explain what you mean by "verify"?want to know how to verify this kind of code
Can you explain what problems are you having?
- 01-22-2012, 04:43 PM #3
Re: Code Detection
I'm having a problem about the SecretCode.txt of having it not specified..
- 01-22-2012, 04:46 PM #4
Re: Code Detection
What do you mean by having it not specified..
Who/what is supposed to "specify" the file?
Do you mean you want to get the name of the file from the user?
You can write a message to the console and use the Scanner class to read the user's response.
- 01-22-2012, 04:55 PM #5
Re: Code Detection
What I mean is that when i compile the code it says no syntax error but when I run it it says "SecretCode.txt(The system cannot find the file specified)(in java.io.FileInputStream)" and that I don't know how to fix it.
- 01-22-2012, 04:59 PM #6
Re: Code Detection
First make sure there is a file with the correct name.system cannot find the file
You can either move the file to where the program is looking for it to be,
or you can change the program to look at the location where the file is.
To see where the program is looking for the file, create an instance of the File class using the file's name n the constructor
and print out the value of the absolute path given by a method in the File class. That will show the location that the program is looking for the file.
- 01-22-2012, 05:01 PM #7
Re: Code Detection
Owkie tnx for the reply..
- 01-23-2012, 03:39 AM #8
Re: Code Detection
I need an exact code to run this program... can somebody help me ?
- 01-23-2012, 03:59 AM #9
Re: Code Detection
Did you try what I suggested for locating where the program was looking for the file? What was printed out?
Another way to find the location is to make different versions of the file and put them into all the possible places the program might look for the file. Have the program print out what it reads so that when it reads the file you can tell by what is printed out which file was read.
- 01-23-2012, 05:40 AM #10
Re: Code Detection
I can't print the file out 'coz our printer has no stock of ink...

will anyway i just want to know how to execute the data that i wanted to input: (7 9 2 7 8 3 5 6).. do you have some code there to do it? 'coz I've tried to run this program since 4 days... that until now I still don't have the idea how to work it out...
- 01-23-2012, 06:42 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Code Detection
Norm is not talking about print the file into a paper. You can print the stuff into the console and see.
- 01-23-2012, 06:43 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Code Detection
Are you sure that you don't have any compile errors in your code? I have just go through your code and found something.
Java Code:// First write the header line to the output file. outP.println("Code Digit Code Digit "Copy");
- 01-23-2012, 07:18 AM #13
Re: Code Detection
will its not that my problem coz i've just correct it just by adding: outP.println("Code Digit Code Digit " + "Copy");
Now my only problem is the ("SecretCode.txt") which says:(The system cannot find the file specified)(in java.io.FileInputStream)"
How?Last edited by Sakura Lime; 01-23-2012 at 07:34 AM.
- 01-23-2012, 07:31 AM #14
- 01-23-2012, 01:01 PM #15
Re: Code Detection
Did you try what I suggested in post #6 for locating where the program was looking for the file?
What was printed out on the console when you executed the program with that change in it?
The path that is printed out will be where you need to place the file you want to read.
- 01-24-2012, 04:51 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Code Detection
- 01-24-2012, 04:51 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Code Detection
Is that your Java class and the text file are in the same folder or in different locations?
- 01-24-2012, 12:15 PM #18
Re: Code Detection
Why so many questions?
What I only asked is the exact code for the expected output...
Now if you don't understand what I'm trying to explain, then try to run the code of the program that I posted. And you will see the result. And if you can't do so, then I have no choice. I'll take it by myself and accept the fact that my brain can't think anymore of some ways to solve this kind of problem.
And no big deal.
Work It,
And I'll Take Care of The Rest
- 01-24-2012, 12:18 PM #19
Re: Code Detection
This will be the expected output:
Code Digit Code Digit Copy
9 9
2 2
7 7
8 8
3 3
5 5
6 6
Message transmitted OK.
And the expected input file will be:
(7 9 2 7 8 3 5 6)Work It,
And I'll Take Care of The Rest
- 01-24-2012, 12:30 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Code Detection
Norm and Eranga are tryiong to explain that you need to figure out where your text file needs to go, or what you need to put into the FileReader constructor to get it to find it.
This line:
is presumably where your exception is coming from.Java Code:Scanner codeFile = new Scanner(new FileReader("SecretCode.txt"));
So, in order to tell where it is looking for the file, try adding this before the above line:
That will tell you where your application thinks the file should be.Java Code:File f = new File("SecretCode.txt"); System.out.println(f.getAbsolutePath()); // You'll have to double check this method call with the API
Once you know that you can then decide to either move the file, or change the text in your code.Last edited by Norm; 01-24-2012 at 03:55 PM. Reason: Fixed name of File object (was secretCode)
Similar Threads
-
Collision Detection?
By Alerhau in forum New To JavaReplies: 39Last Post: 09-07-2011, 04:55 PM -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 12:38 AM -
code for intrusion detection techniques using dataminiing approach
By bvasavi in forum NetworkingReplies: 2Last Post: 12-08-2010, 04:34 PM -
Key Detection
By dilpreet28 in forum Java AppletsReplies: 11Last Post: 06-17-2010, 02:38 AM -
USB Detection
By alanixu in forum New To JavaReplies: 3Last Post: 11-12-2008, 04:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks