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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 42

Thread: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Can Anyone Help me ??

    the problems is i dont know how to store it
    its keeping coming up null in the storing
    also in the switch.

    the strings have to be converted into ints depending on the rate of excellence.

    the code is below

    public void readMarksData(String fileName)throws FileNotFoundException
    {
    File dataFile = new File (fileName);
    Scanner scanner = new Scanner(dataFile);

    //System.out.println(scanner.nextLine());
    cohortName = scanner.nextLine();
    int responces = scanner.nextInt();
    scanner.nextLine();
    //int numberOfStudentResponses=scanner.nextInt();
    //System.out.println("Number of Student Responces " + responces);

    testMarks = new int [responces][10];
    int num;


    for(int add=0; add<8; add++)
    {
    Scanner scanner2 = new Scanner(scanner.nextLine()).useDelimiter("[ ]*(,)[ ]*");
    for (int columns =0; columns<10; columns ++){
    for(int i =0; i<4; i++){
    switch(scanner2.next())
    {
    case "excellent": num=5; break;
    case "very good": num=4; break;
    case "good": num=3; break;
    case "average": num=2; break;
    case "poor": num=1; break;
    default : num=0; break;
    }
    testMarks[add][columns+num] = scanner.nextInt();
    }


    }
    scanner2.close();
    scanner.close();
    }


    }
    Attached Files Attached Files


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

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    its keeping coming up null
    Please copy and paste here the full text of the error messages.

    Is this the same problem?
    http://www.javaprogrammingforums.com...ing-array.html

    Have you tried debugging the code as suggested in post#20 in that thread?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    its in the for area that is more or less the problem
    you can see in the posted image trouble.jpg


     
     
        public void readMarksData(String fileName)throws FileNotFoundException
        {      
            File dataFile = new File (fileName);
            Scanner scanner = new Scanner(dataFile);
     
            //System.out.println(scanner.nextLine());
            cohortName = scanner.nextLine();
            int responces = scanner.nextInt();
            scanner.nextLine();
            //int numberOfStudentResponses=scanner.nextInt();
            //System.out.println("Number of Student Responces " +  responces);
     
            testMarks = new int [responces][10];
            int num;
     
     
            for(int add=0; add<8; add++)
            {
                Scanner scanner2 = new Scanner(scanner.nextLine()).useDelimiter("[ ]*(,)[ ]*");
                for (int columns =0; columns<10; columns ++)
                {
                    System.out.println();
                    for(int i =0; i<4; i++)
                    {
                        System.out.println();
                    switch(scanner2.next())
                    {
                        case "excellent": num=5; break;
                        case "very good": num=4; break;
                        case "good": num=3; break;
                        case "average": num=2; break;
                        case "poor": num=1; break;
                        default : num=0; break;
                    }
                    }
     
     
                }
                System.out.println();
                scanner2.close();
    scanner.close();
     
     
     
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Have you tried debugging the code as suggested in post#20 in that thread?
    You need to add the println statements so you can see what the computer sees when it executes the code.

    If you get errors, Please copy and paste here the full text of the error messages.

    The posted code needs to be formatted so the { and } aline properly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    ive tried everything i could just post all the code from the class n ive put the print statements in nothing is happening its still the switch statement

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Post the code with the println statements that I've suggested be added for debugging.

    Also post the output from when you have executed the code with those printlns.

    You need to learn how to debug your code. I use printlns for debugging and find them very useful.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    its printing out the changed ints that works fine so its changing , but i dont know what to do now ive posted all my code in the file

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Where did you post the version of the code with the println() statements for debugging?

    Where did you post the output for when the code with the debug printlns was executed?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    it keeps coming out blank because of the switch statement parameter so it isnt printing anything

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Post the code with the debug println statements.

    Post the output from the program.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.


  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    If you want help with your problem, this is what you have to do:

    Post the code with the debug println statements.

    Post the output from the program. Not an image.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    there is no out from it

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    I guess asking for two things at the same time was confusing. Let me ask for them one at a time:

    Post the full code with the debug println statements.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

     public void readMarksData(String fileName)throws FileNotFoundException
        {      
            File dataFile = new File (fileName);
            Scanner scanner = new Scanner(dataFile);
     
            //System.out.println(scanner.nextLine());
            cohortName = scanner.nextLine();
            int responces = scanner.nextInt();
            scanner.nextLine();
            //int numberOfStudentResponses=scanner.nextInt();
            //System.out.println("Number of Student Responces " +  responces);
     
            testMarks = new int [responces][10];
            int num;
     
     
            for(int add=0; add<8; add++)
            {
                Scanner scanner2 = new Scanner(scanner.nextLine()).useDelimiter("[ ]*(,)[ ]*");
                for (int columns =0; columns<10; columns ++)
                {
                    for(int i =0; i<4; i++)
                    {
                    switch(scanner2.next())
                    {
                        case "excellent": num=5; break;
                        case "very good": num=4; break;
                        case "good": num=3; break;
                        case "average": num=2; break;
                        case "poor": num=1; break;
                        default : num=0; break;
                    }
     
     
                     System.out.println(num);
                    }
     
     
                }
    scanner2.close();
    scanner.close();
     
     
     
        }
    }


    --- Update ---

    this is printing out 10 0's but with the ints that were changed from strings

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    Ok, now you need to change the code some so that ALL data that is read in is printed before it is used. These statements need to be changed:
              Scanner scanner2 = new Scanner(scanner.nextLine()).
     switch(scanner2.next())

    Read from the Scanner into variables, print the value of the variable and then use that variable where the call to the Scanner class methods are now.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    when does the delimiter come into use because i need to use it

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    That part of the code is not changed. The change is removing the calls to Scanner methods to a separate line,
    printing what was read and then the value read is used where the calls to the Scanner methods were.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    could u show me what u mean kinda making it confusing ive only just started in sept thats why im having trouble
    Scanner scanner2 = new Scanner(scanner.nextLine());
    scanner2.useDelimiter("[ ]*(,)[ ]*");

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    String aStr = scanner.nextLine();    //  read next line into a variable
    System.out.println("aStr="+aStr);   // show what was read
    Scanner scanner2 = new Scanner(aStr);  // set the Scanner to use the line
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    testMarks[add][columns + num] = scanner.nextInt(); - still null , but prints out the first line of the file

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    what does "still null" mean? If there are error messages, copy the full text and print them.

    Change
    testMarks[add][columns + num] = scanner.nextInt();
    to
                    String aStr2 = scanner.next();            //read next tken
                    System.out.println("aStr2="+aStr2);  // show what was read
                    testMarks[add][columns+num] = Integer.parseInt(aStr2); // convert to int
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    i do need to use the delimiter tho to suit the file above tho
    1. need to use the delimiter
    2. i need to read in all the ints on the lines in the file and store them in the 2d array
    3. i need to change the strings at the end of the line and store them in the 2d array
    these are what i need to do

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    need to use the delimiter
    Use the scanner variable to call the useDelimiter() method:
    scanner2.useDelimiter("[ ]*(,)[ ]*");

    This was a problem I talked about in the other thread: The code chained together too many method calls in one statement instead of having one method call per statement.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.

    so how do i the other two , cz of the commas n etc they are the problem

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  2. Reading into a File and storing it into an Array
    By loui345 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 21st, 2012, 10:48 PM
  3. Reading a txt file and storing them in a Java Array
    By FrozenFox in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 27th, 2012, 07:19 AM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  5. How can I detect the end of a line in a file ?
    By lumpy in forum Java Theory & Questions
    Replies: 3
    Last Post: February 18th, 2010, 03:13 AM