3

I have an assignment where I need to display 200 random characters and then ask the use what letter they would like to replace and then replace all those letters. I have the random characters generated, but I am having trouble with replacing the letters. Can someone help lead me in the right direction? Here are some other questions i have:

  • Do I use a for loop for the seek method so it will find all those letters?
  • I also need to display the position each of the letters. Would I use filepointer and would I put that in a loop as well?

Here is my code:

import java.io.*;
import java.util.Scanner;

public class Alphabet {
    public static char getRandomCharacter(char ch1, char ch2) {
        return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
    }

    public static char getRandomUpperCaseLetter() {
        return getRandomCharacter('A', 'Z');
    }

    public static void main(String[] args) throws IOException {

        try (RandomAccessFile raf = new RandomAccessFile("Alphabet.dat", "rw")) {
            raf.setLength(0);

            for (int row = 0; row < 10; ++row){
                for (int col = 0; col < 10; ++col) {
                    raf.writeChar(getRandomUpperCaseLetter());
                }
            }

            raf.seek(0);
            for (int row = 0; row < 10; ++row){
                for (int col = 0; col < 10; ++col) {
                    System.out.print(raf.readChar() +" ");
                }
                System.out.println();
            }

            Scanner Console = new Scanner(System.in);
            System.out.println("Current length of file is: "
                    + raf.length());
            System.out.print("Replace Characters: ");
            String letter = Console.next();
            System.out.print("With Character: ");
            String ch = Console.next();

                for(int j = 0; j < raf.length(); ++j){
                    raf.seek((raf.length()-1)*2);
                    raf.writeChars(ch);
                    System.out.print("Position" + raf.getFilePointer());
                }

            raf.writeChars(ch);
            raf.seek(0);
            for (int row = 0; row < 10; ++row){
                for (int col = 0; col < 10; ++col) {
                }
                System.out.println();
            }
        }
    }
}
  • Is there any code that you have atempted? If so, could you post it? And, I would go about this by using a `while()` loop. – oakTree Apr 21 '15 at 19:20
  • just included my code – Masha Mcintyre Apr 21 '15 at 19:25
  • What is preventing you from reading entire file into string, parse it there with say regex, and then store it into file ? – John Apr 21 '15 at 19:33
  • I keep getting an error when I do Integer.ParseInt(). How would I find all the letters when seek() is for int? @user3360241 Sorry it seem like im asking a lot, but im mentally exhausted from finals and I cant even think straight. – Masha Mcintyre Apr 21 '15 at 19:39

1 Answers1

1

Try replacing for-loop (j < raf.length) with a while loop with the following contents:

long currPointer = 0;
while(currPointer < raf.length()) {
  long currPointer = raf.getFilePointer(); // save current cursor position
  char currentChar = raf.readChar(); // read current char

  if (currentChar == letter) { // if char equals that to be replaced
     raf.seek(currPointer); // step cursor one step back
     raf.writeChar(ch); // replace char
  }

  currPointer = raf.getFilePointer() // store the position of the cursor 

}

EDIT: now the file is traversed character by character, as opposed to byte by byte. Given that various character encodings may not use a constant number of bytes per character, this is the easiest approach.

Basically:

LOOP through all characters in file
    IF current character equals that to be replaced
         step cursor back by one (otherwise you'd be overwriting the next character)
         replace character

Just out of curiosity, what exactly are you trying to achieve with:

raf.seek((raf.length()-1)*2);
Robin Nabel
  • 2,170
  • 1
  • 21
  • 26
  • I was trying to get it to where the user could enter an odd number. before i had: raf.seek(position). It would seek in two's because I used char and that is two bytes. – Masha Mcintyre Apr 21 '15 at 20:16
  • I changed the solution to not traverse the file byte by byte as different encodings may use different byte sizes for characters (Java internal representation does not necessarily equal file representation). I would suggest you use some sort of IDE (Eclipse or IntelliJ) and step through this code to make sure it reads char by char, and compares them correctly. – Robin Nabel Apr 21 '15 at 21:01
  • Your code worked! I had to fix some errors in it, but after that my program works great! Thanks so much – Masha Mcintyre Apr 21 '15 at 21:08