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 16 of 16

Thread: I can't isolate the first four colums y,m,d,t

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I can't isolate the first four colums y,m,d,t

    I'm trying to get the year, month, day and time isolated so I can then I can make it populate in a different format.


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class CSVDateandTime {
     
    	public static void main(String [] args) {
     
    		String fileName = "solar.all";
    		File file = new File(fileName);	
     
    		try {
    			Scanner inputStream = new Scanner(file);
    			while (inputStream.hasNext()){
    				String data = inputStream.next(); //gets a whole line
    				String [] values = data.split(",");
    					System.out.println(data + "***");
    			}
     
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	}
    }
    solar.txt
    Last edited by knockturnal22; September 28th, 2012 at 03:44 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    Can you post the console from when you execute the code that shows what it is doing?

    Add a println() to print out the value of data before it is split.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I'm using Eclipse and i attached a pic of the problem I'm having.
    Error.jpg

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I also attached the file that I was pulling my data.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    Where in the posted code is an array indexed?
    Can you copy and paste here the full text of the error message? You can't copy and paste from an image.


    The code should make sure the index does not go past the end of the array.
    Remember array indexes start at 0 and go to the length-1.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I do not have an array and that's what's throwing me off. I'm using what I learned and it should show me the first column but it's not.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class CSVDateandTime {
     
    	public static void main(String [] args) {
     
    		String fileName = "solar.all";
    		File file = new File(fileName);	
     
    		try {
    			Scanner inputStream = new Scanner(file);
    			while (inputStream.hasNext()){
    				String data = inputStream.next(); //gets a whole line
    				String [] values = data.split(",");
    					System.out.println(values [1] + "***");
    			}
     
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	}
    }

    m***
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at CSVDateandTime.main(CSVDateandTime.java:17)

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I do not have an array
    String [] values =
    ...
    System.out.println(values [1] + "***");
    values is an array.
    How do know that the values array has two elements? 1 is the index for second element

    Where is the print out of the data variable? For debugging you need to do that to show you what is being read.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I guess that's where I'm lost at. I didn't make note of what values meant. Could you help me with pulling out four columns which are the year, month, date and time so I can use that create something else.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    What is printed out when you print out the value of the variable: data?
    Is is what you expect to see?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    Yes that correct. Now trying to get the first 4 columns to display is my current goal.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    What prints out when you print out the value of the variable: data?
    Can you copy and paste here what is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I fixed the problem but now trying to figure out how to isolate the specific columns.
    while (inputStream.hasNext()){
    				String data = inputStream.next(); 
    				System.out.println ( data ) ;
    				String [] values = data.split( ",");
    				System.out.println(values [0] + "*");
    			}

    This is what it prints after the fix.
    y,m,d,t,Incremental Generation Energy (kWh),Incremental Building Demand (kWh),Incremental Insolation (kWh/M^2),Last Generation Energy (kWh),Last Insolation (kWh/M^2),Average Generation Power (kW),Average Irradiance (W/M^2),Average Ambient Temperature (C),Average Cell Temperature (C)

    y*
    2012,8,11,00:00,-0.258,null,null,412854.167,null,-0.243,null,null,null

    2012*

    How would I get it to just show the y,m,d,t?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    get it to just show the y,m,d,t?
    Can you post what you want printed? If the values array has 4 or more elements do you want to see the first 4 elements? To see the contents of the values array use the Arrays class's toString method somethng like this:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));


    The posted code has two println() statements. What is printed by the first one and what is printed by the second one? Add an ID String to each so that you can tell:
    System.out.println ("data="+ data + "<" ) ;
    ...
    System.out.println("Values[0]="+values [0] + "*");
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    knockturnal22 (September 29th, 2012)

  15. #14
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I just want the first 4 elements. I'm trying to get it so I can then turn the y,m,d, t to a code to read it in a standard java day time format.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I just want the first 4 elements.
    Check if values has 4 or more elements. If it does, get the first 4 elements for your data.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Sep 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't isolate the first four colums y,m,d,t

    I'll try that. I think you gave me enough info to work with and figure out the next step.

Similar Threads

  1. [SOLVED] How we can isolate the variables a thread use?
    By Shahram in forum Threads
    Replies: 2
    Last Post: September 19th, 2011, 10:32 AM