简体   繁体   中英

How do I remove odd ASCll characters from a String in a text file?

I hope I am making sense but, I am trying to figure out how to read a String/input in a text file so that I can remove the odd letters in it. So for example 'a' would be 1, 'b' would be 2, 'c' would be 3 and so on. So in my text file I have written in it "Hello, world!". The final output is supposed to be Hll, rld! I really need some help because I'm struggling pretty hard and have just been staring at my computer for some hours getting nowhere, trying to figure this out. Heres what I have so far(basically nothing), I am terrible at fileprocessing.

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;

 public class readodds {
   public static void main (String[]args) throws FileNotFoundException {
     Scanner console = new Scanner(System.in);           
     String letter= new String();
     System.out.println("Name of file: ");
     String inputFile = console.next();

     File file = new File(inputFile);
     Scanner in = new Scanner(file);
     System.out.println(in.nextLine());
     while(in.hasNext()) {
       String line = in.nextLine();
     }
   }
 }

Let the JDK do the heavy lifting for you. It can all be done in one line:

Files.lines(Paths.get(inputFile))
    .map(s -> s.replaceAll("(?i)[acegikmoqsuwy]",""))
    .forEach(System.out::println);
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;

 public class readodds {
   public static void main (String[]args) throws FileNotFoundException {
     Scanner console = new Scanner(System.in);           
     String letter= new String();
     System.out.println("Name of file: ");
     String inputFile = console.next();

     File file = new File(inputFile);
     Scanner in = new Scanner(file);
     while(in.hasNext()) {
       String line = in.nextLine();
       line = line.replaceAll("a","1"); //This replaces every a with a 1
       System.out.println(line);

     }
   }
 }

You can duplicate the call for line.replaceAll("a","1") for every other letter you want to change. If you want to remove a letter: line.replaceAll("a","");

You can loop through the String and print only the even letters by comparison.

    while (in.hasNext()) {
        String line = in.nextLine();
        for (int i = 0; i < line.length(); i++) {
            char letter = line.charAt(i);
            if (Character.toUpperCase(letter) >= 'A' && Character.toUpperCase(letter) <= 'Z' && ('Z' - Character.toUpperCase(letter)) % 2 == 0)
                System.out.print(letter);
        }
    }

you can use asci integer value of character. eg character "a" equivalent int value is 97, b's 98 and so on.. you can do the following code to get transformed line for each of the line of file.. then write the new transformed line to output temporary file

public static void main(String[]  argv) {
        System.out.println(removedOddLetter("Hello, world!"));

    }

    public static String removedOddLetter(String test){
        int aNum='a';
        int ANum='A';
        int zNum='z';
        int ZNum='Z';
        char[] bArr=test.toCharArray();
        StringBuilder outLines=new StringBuilder();
        for(int i=0;i<bArr.length;i++){
            // if the character is alphabet
            if (bArr[i] >= aNum && bArr[i] <= zNum){
                // offSet is zero for odd letter like a
                int offSet=bArr[i]-aNum;
                // if bArr[i] is not odd letter
                if((offSet%2) ==1){
                    outLines.append(bArr[i]);

                }

            }
            //  similarly for Capital characters
            else if (bArr[i] >= ANum && bArr[i] <= ZNum){
                // offSet is zero for odd letter like a
                int offSet=bArr[i]-ANum;
                // if bArr[i] is not odd letter
                if((offSet%2) ==1){
                    outLines.append(bArr[i]);

                }
            }
            else {
                // any other character copy the content
                outLines.append(bArr[i]);
            }
        }
        return outLines.toString();
    }

your While loop will be as

while(in.hasNext()) {

    String line = in.nextLine();
    char[] arr = s.toCharArray ();
    for (char c: arr) {
        if ((c % 2) == 0) {
           System.out.print (c);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM