Results 1 to 11 of 11
  1. #1
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Changing an string from a switch statement and storing it an 2d array

    Hey
    Who ever my help me thank you in advanced

    I have some that using a file and on this file i have 10 ints with commas in between . at the end of the line i have 4 strings which have to be changed into ints and stored into an 2d array along with the other ints.

    can you help me ??


    I have this code >
    Java Code:
    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;
             String now = scanner.nextLine();
                Scanner scanner2 = new Scanner(now);
                scanner2.useDelimiter("[ ]*(,)[ ]*");
             for(int add=0; add<responces; add++)
           
               {
                for (int columns =0; columns<10; columns ++)
                {
                    testMarks[add][columns] =  scanner2.nextInt();
                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 + i] = scanner2.nextInt();
               
                    }
              }
              System.out.println("scanner null" + scanner2 == null);
    scanner2.close();
    scanner.close();
    }
    }
    Attached Files Attached Files
    Last edited by ajw1993; 02-24-2013 at 07:55 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Changing an string from a switch statement and storing it an 2d array

    You didn't bother to reply to the response on your earlier thread: How to add an collection item into an array list in a different class

    Why should we expect any better behavior this time round?

    Also, please go through the following links and edit your post accordingly:
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Changing an string from a switch statement and storing it an 2d array

    because i didnt know i had a reply then sorry

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Changing an string from a switch statement and storing it an 2d array

    To help us help you:
    • Consider editing your original post and surrounding your posted code with [code] [/code] tags so that it retains its formatting and we can then read it.
    • Consider posting the text file that you are trying to analyze.
    • Consider telling us what is wrong with your current code.
    • Consider telling us exactly where you're stuck.


    Much luck!

  5. #5
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Changing an string from a switch statement and storing it an 2d array

    the problems im having is that it keeps saying null when trying to add to the array and im also not sure if it is adding to the array.

    plus im stuck on changing the strings at the end and adding the ints at the start and storing them both.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Changing an string from a switch statement and storing it an 2d array

    Quote Originally Posted by ajw1993 View Post
    the problems im having is that it keeps saying null when trying to add to the array and im also not sure if it is adding to the array.
    The exception message should tell you exactly which line is throwing the NullPointerException. Please find that line and let us know which one it is. A variable that you're trying to use on that line is null.

    plus im stuck on changing the strings at the end and adding the ints at the start and storing them both.
    I'm sorry, I'm not clear on what you mean by this; please clarify.

  7. #7
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Changing an string from a switch statement and storing it an 2d array

    null parts 1.testMarks[add][columns] = scanner2.nextInt();
    2. switch(scanner2.next())
    3. testMarks[add][columns] =scanner2.next ; (in the switch statement)


    both of these have to be stored in the same 2d array:
    1. the ints at the start of the line
    2 . the strings at the end of the line which have to be changed into ints ( by a switch statement)
    Last edited by ajw1993; 02-24-2013 at 07:11 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Changing an string from a switch statement and storing it an 2d array

    Quote Originally Posted by ajw1993 View Post
    null parts 1.testMarks[add][columns] = scanner2.nextInt();
    2. switch(scanner2.next())
    3. testMarks[add][columns] =scanner2.next ; (in the switch statement)
    OK, are you sure that you've created a Scanner object? You can easily test this by posting
    Java Code:
    System.out.println("scanner2 null? " + (scanner2 == null));
    And thanks for the edit.

  9. #9
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Changing an string from a switch statement and storing it an 2d array

    String now = scanner.nextLine();
    Scanner scanner2 = new Scanner(now);
    scanner2.useDelimiter("[ ]*(,)[ ]*");

    am i missing something from there because i have put that system out statement and nothing prints out to the terminal and says null on > testMarks[add][columns + i] = scanner2.nextInt();

    i have posted the entire code for the method
    Last edited by ajw1993; 02-24-2013 at 07:56 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Changing an string from a switch statement and storing it an 2d array

    If still null on that line and you're sure you've created the Scanner object, then test if the array is null on the line above where the NPE is thrown.

  11. #11
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Changing an string from a switch statement and storing it an 2d array

    its coming out false when trying to test it and it keeps saying it when im trying to store it for the second time

    would i have to change anything when trying to store it e.g. scanner2.nextInt() to ?

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 09:12 PM
  2. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  3. Replies: 6
    Last Post: 03-17-2012, 05:15 AM
  4. Changing a String array into a String?
    By BennyJass in forum New To Java
    Replies: 6
    Last Post: 01-16-2011, 03:42 PM
  5. storing a string in an array
    By tiyani in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 08:25 PM

Posting Permissions

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