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

Thread: nextLine().split(",") not work correctly

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy nextLine().split(",") not work correctly

    Hi
    there is a csv file that contain 10 rows and two columns.
    i want to convert this csv file to array.
    each cell contains several line of string.
    but the output is wrong.
    after printing output, each cell of array contains a line of one cell of csv file.


    for example csv file contain

    book, this book is good. that book is good.
    cook, cooking is enjoyable.

    after printing output of array:

    book,this one is good
    that book is good, null


    what is wrong with this code?

    code is :


       public static void main(String[] args) throws Exception {
     
              Scanner main = new Scanner(new File("C:\\Users\\a\\Desktop\\book1.csv"));
     
              int line=0;
              String [][] temp1=new String [10][2];
              while (main.hasNext()) {
              temp1[line]=main.nextLine().split(",");
              line++;
              }
    }


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    383
    My Mood
    Angelic
    Thanks
    27
    Thanked 55 Times in 49 Posts

    Default Re: nextLine().split(",") not work correctly

    I am quite surprised that this:

    Scanner main

    Is not causing at least a warning to generate because of:

    public static void main(String[] args) throws Exception {

    Your input object for the Scanner class really should be called something other than "main".
    Main is the globally called at execution - and that is really all it should be used for.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    483
    Thanks
    0
    Thanked 92 Times in 77 Posts

    Default Re: nextLine().split(",") not work correctly

    You can use main as a name for any variable or method. You can also declare several main methods in your code.
    There is absolutely no problem with that.

    @saeedeh:
    I do not see any critical errors in your code, maybe show us the code that generates the wrong output. It might be that your printing is not quite correct.

  4. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    Ada Lovelace (June 4th, 2014), saeedeh (June 4th, 2014)

  5. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: nextLine().split(",") not work correctly

    thanks.

    i got this error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at javaapplication19.NewClass.main(NewClass.java:27)
    Java Result: 1
    Last edited by saeedeh; June 5th, 2014 at 11:00 AM.

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    483
    Thanks
    0
    Thanked 92 Times in 77 Posts

    Default Re: nextLine().split(",") not work correctly

    Well, that error makes a lot of sense.
    Look at your code, you declare an array with a size of 10.

    But you try to populate it with a while-loop that runs for as long as your scanner can read more input.
    So there is a possibility that your scanner will read in more lines then 10, in this case an ArrayIndexOutOfBoundsException is inevitably.
    Even if you are sure, that your file contains no more then 10 lines, its still a bad practice in general to not check for these kinds of errors.

    What you could do is change the condition of your loop from:
    main.hasNext()
    to
    main.hasNext() && line < temp1.length
    this way you can be sure that you will never read more lines then your array can hold.

    Furthermore, if you still wish to keep an exception in the case that your file has too many lines, you could add an if-then-else in which you throw your own, custom exception (or RuntimeException) that describes the problem in greater detail.

  7. The Following User Says Thank You to Cornix For This Useful Post:

    saeedeh (June 4th, 2014)

  8. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: nextLine().split(",") not work correctly

    thanks again.
    I correct that code and this error is corrected,
    but main problem is still remain.
    wrong data are stored in data.
    the split character is "," but this character is ignored. and wrong data are stored in array.

  9. #7
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    483
    Thanks
    0
    Thanked 92 Times in 77 Posts

    Default Re: nextLine().split(",") not work correctly

    Could you show us your code right now, both for populating the array and for printing the result?
    It would also be useful if you could show us the output and the expected output.

    Until then I can not help you.

  10. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: nextLine().split(",") not work correctly

    i found the answer. i should write a file reader by using a opencsv.
    because scanner takes each line in nextLine command.
    and each cell contains several line that should be consider as one cell but in scanner each line of one cell goes to different cells.

Similar Threads

  1. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  2. String.split"char"; error
    By bean in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 3rd, 2013, 05:30 PM
  3. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM
  4. Replies: 4
    Last Post: October 3rd, 2012, 07:40 AM
  5. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 17th, 2012, 03:25 AM