Java woes

  • Roderick Ramsay
    Participant

    Hi
    I’ve moved to a school that uses Java as their programming language of choice and I am not a good Java programmer.

    Does anyone have resources for reading in file data to arrays?
    I’ve done the usual google searching and all I can find are snippets but I’d like to see a whole program if possible. It’s the way I learn.

    thanks

    Paul Gardner
    Participant

    Hi Roderick,

    here’s a simple demo that reads a CSV file into an array of strings. Each item in the file becomes one array element. On my setup (java on Windows using notepad and command line compiling and executing) the file is compiled then executed with:

    javac readfile.java

    java -cp . readfile

    and “thefile.csv” is in the same directory as the complied .class file

    import java.io.BufferedReader;
    import java.io.FileReader;

    public class readfile {
    public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("thefile.csv"));
    String line = null;

    while ((line = br.readLine()) != null) {
    String[] values = line.split(",");
    for (String str: values) {
    System.out.println(str);
    }
    }
    br.close();
    }
    }

    The code comes from http://www.java2s.com which has lots of demos.

    Paul

    (Pasting code here might lose the indentation but https://codebeautify.org/javaviewer will resore the layout)

     

     

    Paul Gardner
    Participant

    Hi Roderick,

    I’ve just uploaded a larger demo to the ‘documents’ part of the forum. This Java program demonstrates console i/o, file i/o, a simple class to store structured data and some error handling techniques useful for catching user input errors, file i/o and data conversion errors.

    Paul

     

    Peter W Donaldson
    Participant

    Hi Roderick,

    the following interactive online revision book for the CS AP JAVA exam may help you to quickly transfer what you know over to JAVA and parts of it may also be useful as additional practice for your pupils. Unfortunately, it doesn’t cover file handling but it does cover everything from N5 up to AH including OO and Recursion in JAVA.

    http://interactivepython.org/runestone/static/JavaReview/index.html

    There is nothing to install and it lets you code directly in the browser.

    It has lots of short exercises which include a range of self marking interactive questions that involve code identification and description, problem solving worked examples and code writing exercises using parsons puzzles as well as partial and full code writing from scratch. There is also discussion of common mistakes and how to avoid them.

    It’s a great illustration of many of the teaching approaches that were explored in the PLAN C programme. This isn’t surprising as it was created by Georgia Tech where a lot of the current active research into worked examples and parsons puzzles is being carried out.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.