Results 1 to 3 of 3
  1. #1
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Help Data Streams

    Hello everyone, here is my code, I'm trying to make this program some data from the user and print it in goals.txt, I can not see any error, but when I run it, show : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
    at GoalsData.main(GoalsData.java:7)

    and nothing get print in the document. Thank you in advance.

    Does anyone has an idea of what am I doing wrong?, Thank you in advance


    Java Code:
    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.PrintWriter;
    public class GoalsData {
    	public static void main (String [] args){
                    if (args.length != 1){
    		    System.out.println("Track goal");
    		    System.exit(0);
    		int goal = 0;
    		int goal_add=Integer.parseInt(args[0]);
    		try {
    		//read file
          Scanner in = new Scanner (new FileReader("goals.txt"));
          goal= in.nextInt();
    		goal+=goal_add;
    		//write on file
    		PrintWriter out = new PrintWriter("goals.txt");
    		out.println(goal);
    		out.close();
    		System.out.println(goal);
    		} catch (Exception e){
    		System.out.println("test");
    		}
    	}
    }
    Last edited by Tolls; 10-15-2013 at 09:48 AM. Reason: Fixed code tags

  2. #2
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Re: Help Data Streams

    Hello everyone, here is my code, I'm trying to make this program some data from the user and print it in goals.txt, I can not see any error, but when I run it, show : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
    at GoalsData.main(GoalsData.java:7)

    and nothing get print in the document. Thank you in advance.

    Does anyone has an idea of what am I doing wrong?, Thank you in advance


    Java Code:
    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.PrintWriter;
    public class GoalsData {
    public static void main (String [] args){
    if (args.length != 1){
    System.out.println("Track goal");
    System.exit(0);
    int goal = 0;
    int goal_add=Integer.parseInt(args[0]);
    try {
    //read file
    Scanner in = new Scanner (new FileReader("goals.txt"));
    goal= in.nextInt();
    goal+=goal_add;
    //write on file
    PrintWriter out = new PrintWriter("goals.txt");
    out.println(goal);
    out.close();
    System.out.println(goal);
    } catch (Exception e){
    System.out.println("test");
    }
    }
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Help Data Streams

    Even correcting the code tags in your first post doesn't really help.

    Sort out your indentation so we can see where the various bits of your code sit, because it looks to me like that lot has the brackets all wrong.
    At first glance if your args does not have only 1 value then nothing at all happens, otherwise it will print "Track goal" and then exit.
    No chance there of any exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JFrame frozen while updating data from streams?
    By SmugMug in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 05:57 AM
  2. Best practices in handling big data streams
    By rsantana in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2011, 03:23 PM
  3. Socket, streams and data types?
    By Tomtefaen in forum Networking
    Replies: 0
    Last Post: 01-14-2011, 11:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •