开发者

Storing data from text into variables

开发者 https://www.devze.com 2023-03-03 07:12 出处:网络
I\'m trying to read data from a text file and store various data types into variables. Assuming the txt file is in the following form;

I'm trying to read data from a text file and store various data types into variables. Assuming the txt file is in the following form;

a 1000 1

b 2000 2

c 3000 3

Now, I'm trying to store the characters into separate variables and the integers into separate variables.

My attempt so far has involved me reading the text file into a string and then using string tokenizer to store each element into an array list. I kind of have a general idea on how to go about doing this; check to see if the element in the list is a character, if so, store it into a character variable or else if its an integer, store it into an int. However, I am not familiar with the methods on identifying whether something is a string or an integer, like isString, isInteger, etc etc. Could anyone please give me some advice on how to go about doing this?

My code as follows:

public class copyToString {


    public static void main(String[] args) {

        String fileSpecified = args[0];

        fileSpecified = fileSpecified.concat(".txt");
        char [] content = new char[1024];

        System.out.println ("file Specified = " + fileSpecified);

        String container;
        ArrayList <String> words = new ArrayList<String> ();


        try {
            FileReader fr = new FileReader (fileSpecified);
            BufferedReader br = new BufferedReader (fr);
            StringBuilder builder = new StringBuilder();

            int read = 0;
            while ((read = br.read(content, 0, content.length)) > 0) {
                builder.append(content, 0, read);
            }

            container = builder.toString();

            StringTokenizer tok开发者_开发技巧enizer = new StringTokenizer (container);
            while (tokenizer.hasMoreTokens()) {
                words.add(tokenizer.nextToken());
            }
            fr.close();
        } catch (IOException e) {
            System.out.println (e.getMessage());
        }

        for (int i = 0; i < words.size(); i++) {
            System.out.println ("words = " + words.get(i));


        }



    }



}

Thanks


I would recommend the Apache Commons Lib here very much. Because this lib has a

  • StringUtils class which has a large method base for checking the content of string
  • IOUtils class for reading files easily

Additionally I would use simple regular expression groups to identify your text parts.

See Pattern and Matcher class for details. (Regex for Words: "\w" Digits "\d")


If you really don't know what each type might be, you have to store every field as String as a number like 1000 could be a short, int, long, float, double or String. Is 1 a number , String or the character '1'? Without context you cannot know what each type is. a, b and c could be a numbers in hexidecimal. ;)


It would take me longer to say what I would do differently, than it would to re-write the code. ;)

public class CopyToString {
    static class Line {
        String word;
        int num1, num2;

        Line(String word, int num1, int num2) {
            this.word = word;
            this.num1 = num1;
            this.num2 = num2;
        }

        @Override
        public String toString() {
            return "Line{" + "word='" + word + '\'' + ", num1=" + num1 + ", num2=" + num2 + '}';
        }
    }

    public static void main(String... args) throws IOException {
        String fileSpecified = args[0] + ".txt";
        System.out.println("file Specified = " + fileSpecified);
        BufferedReader br = new BufferedReader(new FileReader(fileSpecified));

        List<Line> lines = new ArrayList<Line>();
        for (String line; (line = br.readLine()) != null;) {
            Scanner scanner = new Scanner(line);
            lines.add(new Line(scanner.next(), scanner.nextInt(), scanner.nextInt()));
        }
        br.close();

        for (Line l : lines)
            System.out.println(l);
    }
}

prints

file Specified = text.txt
Line{word='a', num1=1000, num2=1}
Line{word='b', num1=2000, num2=2}
Line{word='c', num1=3000, num2=3}
0

精彩评论

暂无评论...
验证码 换一张
取 消