简体   繁体   中英

why doesn't this java method work? it should eliminate empty spaces in Strings

here's the method :

public static String normalizza(String x) {

    for (int i = x.length(); i > 0; i--) {
        if (x.substring(i, x.length()).equalsIgnoreCase(" ")) {
            x = x.substring(0, i);
        }
    }

    return x;
}

i should read a String from a random access file and eliminate empty spaces to find the object position

these are the attributes of the class :

public class Iscritto {
private int id;
private String nome;
private String cognome;
private String dataNascita;

this is the search method for the attribute "nome" ( the method "normalizza" works with this one ) :

private static void cercaNome() {
    Iscritto a = new Iscritto();
    try {
        File file = new File("C:\\temp\\iscritti.dat");
        RandomAccessFile ra = new RandomAccessFile(file, "r");
        String nome = JOptionPane.showInputDialog("Inserisci nome da cercare:");
        ra.seek(0);
        a.leggi(ra);
        String nomeControllato = a.getNome();
        nomeControllato = normalizza(nomeControllato);
        int conta = 0;

        if (nomeControllato.equalsIgnoreCase(nome)) {
            int b = (int) ra.getFilePointer() - 2;
            int position = b / 153;
            ra.seek(position * 153);
            a.leggi(ra);
            System.out.println("iscritto:   " + a);
        } else {
            while (!nomeControllato.equalsIgnoreCase(nome)) {
                conta++;
                ra.seek(conta * 153);
                a.leggi(ra);
                nomeControllato = a.getNome();
                nomeControllato = normalizza(nomeControllato);

                if (nomeControllato.equalsIgnoreCase(nome)) {
                    int b = (int) ra.getFilePointer() - 2;
                    int position = b / 153;
                    ra.seek(position * 153);
                    a.leggi(ra);
                    System.out.println("iscritto:   " + a);
                }

            }
        }
        ra.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

this is the search method for the attribute "data" ( normalizza doesn't work for this one ) :

private static void cercaData() {
    Iscritto a = new Iscritto();
    try {
        File file = new File("C:\\temp\\iscritti.dat");
        RandomAccessFile ra = new RandomAccessFile(file, "r");
        String data = JOptionPane.showInputDialog("Inserisci data da cercare (example : 12-MAG-2018):");
        ra.seek(0);
        a.leggi(ra);
        String dataControllata = a.getDataNascita();
        dataControllata = normalizza(dataControllata);
        int conta = 0;

        if (dataControllata.equalsIgnoreCase(data)) {
            int b = (int) ra.getFilePointer() - 2;
            int position = b / 153;
            ra.seek(position * 153);
            a.leggi(ra);
            System.out.println("iscritto:   " + a);
        } else {
            while (!dataControllata.equalsIgnoreCase(data)) {
                conta++;
                ra.seek(conta * 153);
                a.leggi(ra);
                dataControllata = a.getDataNascita();
                dataControllata = normalizza(dataControllata);

                if (dataControllata.equalsIgnoreCase(data)) {
                    int b = (int) ra.getFilePointer() - 2;
                    int position = b / 153;
                    ra.seek(position * 153);
                    a.leggi(ra);
                    System.out.println("iscritto:   " + a);
                }

            }
        }
        ra.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

if i search a "data" and it's not the last of the file it will give me an infinite "null" output

If you want to eliminate empty spaces at the end of your String, simply use the trim() method.

System.out.println(" my String  ".trim()); // prints 'my String'

If you need to eliminate all whitespaces in your string, then use replace(String, String)

System.out.println(" my String  ".replace(" ", "")); // prints 'myString'

Assuming you have a reason not to use String.trim(), you could do a much simpler implementation:

start i from the end of string, and look backward at each .charAt(i) until != ' '. Then substring just once, cutting after that non-space char.

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