Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: When a button is clicked: get my Data!!!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default When a button is clicked: get my Data!!!

    Hey There..

    I'm having a problem occurring within my code.

    I have a txt file containing jibberish called testingfile.txt


    testingfile.txt contains:

    hithere34|where 293|21|3|9
    heythere5|where 633|25|4|9
    hellothere66|where 918|21|7|9

    Now when the 'List' button is clicked I want it to just print in the console the data within the testingfile.txt (I want it to print out in the console so I know that it is being called).

    I tried this but it keeps throwing an IOException.. :

        public myGrades() {
            initComponents();
     
            txtAreaActions number = new txtAreaActions();
            btnList.addActionListener(number);
     
        }
     
        public class txtAreaActions implements ActionListener {
     
            public void actionPerformed(ActionEvent e) {
                try {
                    if (e.getSource() == btnList) {
                        Scanner scanner = new Scanner(new File("testingfile.txt"));
                        scanner.useDelimiter("\\|, \\s, ");
     
                        while (scanner.hasNextLine()) {
                            String s = scanner.next();
                            parseRecord(s);
                        }
     
                        scanner.close();
                    }
                } catch (IOException error) {
                    System.out.println("Error");
                }
            }
        }
     
        public void parseRecord(String record) {
            Scanner recIn = new Scanner(record);
            recIn.useDelimiter("\\|, \\s");
     
            String area1 = recIn.next();
            String area2 = recIn.next();
            int num1 = Integer.parseInt(recIn.next());
            int num2= Integer.parseInt(recIn.next());
            int num3 = Integer.parseInt(recIn.next());
     
            double total = (num1 / num2) * num3;
     
            System.out.println(area1 + area2);
            System.out.printf("%.2f%n", total);
        }

    Any clues?

    Thanks!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: When a button is clicked: get my Data!!!

    It might help if you obtain more information than merely "Error".

    } catch (IOException error) {
        //System.out.println("Error");
        error.printStackTrace();
    }

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: When a button is clicked: get my Data!!!

    Got this:

    java.io.FileNotFoundException: testingfile.txt (The system cannot find the file specified)

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: When a button is clicked: get my Data!!!

    In fact you will have got a stack trace that indicates where the problem occured: I'm guessing in the line that initialised scanner.

    You could check the full path of the file you are trying to open (there is a File method that returns this) before you inialise the scanner.

    File file = new File("testingfile.txt");
    System.out.println("Using " + file.xxx());
    Scanner scanner = ...

    (Sorry, I'm posting from my phone and it's too painful to look up the method name...)

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    copeg (March 14th, 2012)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: When a button is clicked: get my Data!!!

    Quote Originally Posted by pbrockway2 View Post
    You could check the full path of the file you are trying to open (there is a File method that returns this)...
    (Sorry, I'm posting from my phone and it's too painful to look up the method name...)
    getAbsolutePath()

    Your program must know where the file is...so you must specify the full or relative path to that file (relative to where you are running your java app). Assuming your class posted above is not in a package, the file must be located in the same directory as your class file.

  7. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: When a button is clicked: get my Data!!!

    if it is inside a package how would I get the relative path name?

    or would the getAbsolutePath() do the trick

    i tried this:

    public void actionPerformed(ActionEvent e) {
                try {
                    if (e.getSource() == btnList) {
                        File file = new File("testingfile.txt");
                        Scanner scanner = new Scanner(file.getAbsoluteFile());
                        scanner.useDelimiter("\\|, \\s, ");
     
                        while (scanner.hasNextLine()) {
                            String s = scanner.next();
                            parseRecord(s);
                        }
     
                        scanner.close();
                    }
                } catch (IOException error) {
                    System.out.println("Error");
                }
            }
        }

    No luck
    Last edited by aStudentofJava; March 15th, 2012 at 12:19 AM.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: When a button is clicked: get my Data!!!

    Where is the file relative to your class? Is your class in a package?

Similar Threads

  1. Replies: 9
    Last Post: February 20th, 2012, 04:02 PM
  2. How to display data against row clicked on html table?
    By reubenmk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 16th, 2012, 03:34 AM
  3. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 02:49 PM
  4. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM
  5. [SOLVED] Executable .jar file isn’t launched after being double-clicked
    By voltaire in forum Java Theory & Questions
    Replies: 6
    Last Post: May 18th, 2010, 03:37 PM