Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Code Detection

  1. #1
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Code Detection

    Guys I need help... do somebody knows how to verify the input file "SecretCode.txt"? this is my 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.
    I really badly want to know how to verify this kind of code...
    Last edited by Sakura Lime; 01-23-2012 at 08:34 AM. Reason: Added [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: Code Detection

    want to know how to verify this kind of code
    Please explain what you mean by "verify"?

    Can you explain what problems are you having?

  3. #3
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code Detection

    I'm having a problem about the SecretCode.txt of having it not specified..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default 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.

  5. #5
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default 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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: Code Detection

    system cannot find the file
    First make sure there is a file with the correct name.
    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.

  7. #7
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code Detection

    Owkie tnx for the reply..

  8. #8
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code Detection

    I need an exact code to run this program... can somebody help me ?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default 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.

  10. #10
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default 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...

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Code Detection

    Norm is not talking about print the file into a paper. You can print the stuff into the console and see.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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");

  13. #13
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default 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 08:34 AM.

  14. #14
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code Detection

    Quote Originally Posted by Norm View Post
    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.
    Having it print out will still not recognize the file. Maybe there still another code for that to be read.
    Last edited by Sakura Lime; 01-23-2012 at 09:10 AM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default 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.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Code Detection

    Quote Originally Posted by Sakura Lime View Post
    will its not that my problem coz i've just correct it just by adding: outP.println("Code Digit Code Digit " + "Copy");
    Useless.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Code Detection

    Is that your Java class and the text file are in the same folder or in different locations?

  18. #18
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default 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

  19. #19
    Sakura Lime's Avatar
    Sakura Lime is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default 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

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default 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:
    Java Code:
    Scanner codeFile = new Scanner(new FileReader("SecretCode.txt"));
    is presumably where your exception is coming from.
    So, in order to tell where it is looking for the file, try adding this before the above line:
    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
    That will tell you where your application thinks the file should be.
    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 04:55 PM. Reason: Fixed name of File object (was secretCode)

Page 1 of 2 12 LastLast

Similar Threads

  1. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  2. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 01:38 AM
  3. Replies: 2
    Last Post: 12-08-2010, 05:34 PM
  4. Key Detection
    By dilpreet28 in forum Java Applets
    Replies: 11
    Last Post: 06-17-2010, 03:38 AM
  5. USB Detection
    By alanixu in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •