#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2

    Separating by character without delimiter


    My assignment is to, using methods, get information from a text file with an ID number and scantron values for answers that that student made.
    I must then calculate their scores and such.

    It's just this first method is killing me.
    I don't know what it should return and I don't know how to make it return it.

    I will need to separate each of the 15 answers into 15 individual answers, I think, but I don't know how to do that. There is no space in between them. The text file reads like this:

    Code:
    4563123,112211324135412
    2312311,222121324135211
    2312345,112211324135421
    5527687,212111313124412
    7867567,111111111111111
    The number before the comma is the ID and the number after is the series of answers.

    How can I separate each of those?

    This is what I have:

    Code:
     public static String fileStuff () throws IOException
     {
         Scanner s = new Scanner(System.in);
         
         System.out.println("What is the name of the file?");
                             File fileName = new File (s.nextLine());
          
         Scanner inFile = new Scanner(fileName);
         if (fileName.exists()){
            File results = new File ("results.txt");
            FileWriter fw;
            fw = new FileWriter(results, true);
            PrintWriter outFile = new PrintWriter ("results.txt");
         
            PrintWriter outputFile = new PrintWriter(fw);
         
            String [][] answers = new String [50][50];
        
         while (inFile.hasNext()){
             String line = inFile.nextLine();
             String [] pieces = line.split(",");
            
             
             
             for (int x = 0; x < pieces.length -1; x++){
              //  if(x+1<pieces.length){
                 String [] WID = new String [50];
                 
                 answers[x][x+1] = pieces [x+1];
                 WID [x] = pieces[x]; 
                
                 
                 //print WID
                 outputFile.println("WID: " + WID [x]);
                 
                 //Print answers
                 outputFile.println(answers[x][x+1]);
                 outputFile.println(" ");
             //   }// end if
                 /*
           String trueOrFalse = answers[x+1].substring(0,6);
           String multipleChoice = answers[x+1].substring(6,15);
           
                String trueOrFalse0 = trueOrFalse.substring(0,1);
                String trueOrFalse1 = trueOrFalse.substring(1,2);
                String trueOrFalse2 = trueOrFalse.substring(2,3); 
                String trueOrFalse3 = trueOrFalse.substring(3,4);
                String trueOrFalse4 = trueOrFalse.substring(4,5);
                
                */
             }// end for
               
         }
       System.out.print(answers[0][3]);
         inFile.close();
         outFile.close();
         outputFile.close();
         
    
      
         return ("");
    } //end if
         
         else {
          System.out.append("File does not exist! Try again:");
          return ("");}
         
        
         
     } //end fileReader
     
    
     
     public static void printGrade (char a){
    
    System.out.println("Grade: " + a + "\n");
    }
     
     public static int numberCorrect (int  number){
         //grade a single quiz and return numberCorrect
         return(0);
     }// grade
     
      static void average (int h ){
          //calculate and return average
          return;
      }//average
      
       static void display (int h ){
           //display 
           return;
       }
     static void sort (int [ ] n)
     {
         return;
     } //end swap
    }

    1. How can I make each answer its own string?
    2. How can I get this method to return all of those strings for calculation in another method?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    Because it should be 15 different Strings for each student that this return statement should return.

    and then I have to determine if it's correct or not.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2

    Substring


    So I have figured out one way to do it using substrings like so:

    Code:
    String mChoice0 = answers[x+1].substring(5,6);  
                String mChoice1 = answers[x+1].substring(6,7);
                String mChoice2 = answers[x+1].substring(7,8);
                String mChoice3 = answers[x+1].substring(8,9);  
                String mChoice4 = answers[x+1].substring(9,10);
                String mChoice5 = answers[x+1].substring(10,11);
                String mChoice6 = answers[x+1].substring(11,12);
                String mChoice7 = answers[x+1].substring(12,13);
                String mChoice8 = answers[x+1].substring(13,14);
                String mChoice9 = answers[x+1].substring(14,15);
                
           
                String trueOrFalse0 = trueOrFalse.substring(0,1);
                String trueOrFalse1 = trueOrFalse.substring(1,2);
                String trueOrFalse2 = trueOrFalse.substring(2,3); 
                String trueOrFalse3 = trueOrFalse.substring(3,4);
                String trueOrFalse4 = trueOrFalse.substring(4,5);
                 
                 //print WID
                 outputFile.println("WID: " + WID [x]);
                 
                 //Print truefalse
                 outputFile.println(trueOrFalse0);
                 outputFile.println(trueOrFalse1);
                 outputFile.println(trueOrFalse2);
                 outputFile.println(trueOrFalse3);
                 outputFile.println(trueOrFalse4);
                 
                 //print MC
                 outputFile.println(mChoice0);
                 outputFile.println(mChoice1);
                 outputFile.println(mChoice2);
                 outputFile.println(mChoice3);
                 outputFile.println(mChoice4);
                 outputFile.println(mChoice5);
                 outputFile.println(mChoice6);
                 outputFile.println(mChoice7);
                 outputFile.println(mChoice8);
                 outputFile.println(mChoice9);
    But inside another method, now I'm trying to separate a single answer and be able to determine if it's right using an answer key.

    So far I have this:

    Code:
    public static void grade () throws FileNotFoundException{
    
    //System.out.println("Grade: " + a + "\n");
         
         int answer[] = new int [15];
         
         
         
    //1) open a connection to the file
    Scanner inFile = new Scanner (new File ("results.txt") ); 
    
    //2) read input from the file
    
    
    while (inFile.hasNext()) {
        Scanner s = new Scanner(System.in);
    String line = inFile.nextLine();
    for (int x = 0; x < answer.length; x++) {
    if (line.charAt(x) != 'W' || answer[x] >0) {
        answer[x] = line.charAt(x);
    }//end if
    System.out.println(answer[x]);
    }// end for
    }//end while
    
    //3) close the file 
    inFile.close();  
    }//end grade
    But I don't know how to make it look down on the next line if it doesn't find what it's looking for.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    how to make it look down on the next line
    Can you explain? The code is using the Scanner class's nextLine() method which reads the next line.

    The code needs to be properly formatted to make it easier to read and understand. Statements within {}s should be indented 3-4 spaces to show the logic.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    I'm sorry. You're right. That wasn't the problem.
    It just seemed to be.

    Now I'm getting a null pointer exception and I don't understand how.


    Code:
     public static void grade () throws FileNotFoundException, IOException{
    
    //System.out.println("Grade: " + a + "\n");
    String answer[] = new String [17];
    String answerNum [] = new String [17];
    String correctAnswers [] = {"1","1", "2","2","1","1","3","2","4","1","3","5","4","1","2"};
    
    int count = 0;
         
         
    //1) open a connection to the file
    Scanner inFile = new Scanner (new File ("onePerLine.txt") ); 
    
       File results = new File ("results.txt");
       FileWriter fw;
       fw = new FileWriter(results, true);
       PrintWriter outFile = new PrintWriter ("results.txt");
       PrintWriter outputFile = new PrintWriter(fw);
         
    
    
    //2) read input from the file
    
    int numCorrect;
    while (inFile.hasNext()) {
    
        for (int x = 0; x < 15; x++) {
        
            answer[x] = inFile.nextLine();
            
           
            //count correct
            if (answer[0].equals(correctAnswers[0])){
                count ++;
            }
            if (answer[1].equals(correctAnswers[1])){
                count ++;
            }
            if (answer[2].equals(correctAnswers[2])){
                count ++;
            }
            if (answer[3].equals(correctAnswers[3])){
                count ++;
            }
            if (answer[4].equals(correctAnswers[4])){
                count ++;
            }
            if (answer[5].equals(correctAnswers[5])){
                count ++;
            }
            if (answer[6].equals(correctAnswers[6])){
                count ++;
            }
            if (answer[7].equals(correctAnswers[7])){
                count ++;
            }
            if (answer[8].equals(correctAnswers[8])){
                count ++;
            }
            if (answer[9].equals(correctAnswers[9])){
                count ++;
            }
            if (answer[10].equals(correctAnswers[10])){
                count ++;
            }
            if (answer[11].equals(correctAnswers[11])){
                count ++;
            }
            if (answer[12].equals(correctAnswers[12])){
                count ++;
            }
            if (answer[13].equals(correctAnswers[13])){
                count ++;
            }
            if (answer[14].equals(correctAnswers[14])){
                count ++;
            }
            if (answer[15].equals(correctAnswers[15])){
                count ++;
            }
    
            numCorrect = count;
    
    
            outputFile.print("hello");
    	outputFile.println(numCorrect);
           
    
       }// end for
    //3) close the file 
    
    
    }//end while
    inFile.close();  
    outFile.close();
    outputFile.close();
    fw.close();
    
    }//end grade
    I improved the tabbing.

    it's reading from a file I called onePerLine.txt that just contains the answers from the students one by one, one per line.


    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at proj5.Proj5.grade



    input :
    Code:
    WID: 4563123
    1
    1
    2
    2
    1
    1
    3
    2
    4
    1
    3
    5
    4
    1
    2
    WID: 2312311
    2
    2
    2
    1
    2
    1
    3
    2
    4
    1
    3
    5
    2
    1
    1
    WID: 2312345
    1
    1
    2
    2
    1
    1
    3
    2
    4
    1
    3
    5
    4
    2
    1
    WID: 5527687
    2
    1
    2
    1
    1
    1
    3
    1
    3
    1
    2
    4
    4
    1
    2
    WID: 7867567
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1


    I can't figure out why it's null. Any error but null pointer would make sense. And it only says it's null on the second statement it runs.

    (eg. if (answer[1].equals(correctAnswers[1])){
    count ++;
    } instead of with answer [0])
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    I mean, I understand that they will all be nulls until the for loop gets to them, but shouldn't it just not satisfy the if condition?
    Can you not compare a null to a string?
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Exception in thread "main" java.lang.NullPointerException
    at proj5.Proj5.grade
    The error message left off the line number where the error occured. Look at that line and find the variable with the null value then backtrack in the code to see why that variable does not have a valid value.

    The code could use a loop with one if statement in place of those 15 if statements.
    The loop that is there doesn't use the value of x anywhere inside the loop.
    The for loop should use the .length attribute of the array and not the hardcoded value 15
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    Originally Posted by NormR
    The error message left off the line number where the error occured. Look at that line and find the variable with the null value then backtrack in the code to see why that variable does not have a valid value.

    The code could use a loop with one if statement in place of those 15 if statements.
    The loop that is there doesn't use the value of x anywhere inside the loop.
    The for loop should use the .length attribute of the array and not the hardcoded value 15
    Thank you for your help. This project is killing me.
    I hardcoded 15 just to make sure it wasn't something I did wrong.
    I have made it one if statement, but now it won't raise the counter. When I print it, it comes out to 0.
    Why would that be?
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    it comes out to 0.
    Why would that be?
    The increment statement was not executed.

    Add a println statement that prints out the value of the variable that was read so you can see what the computer sees when the code is executed.

    Post the new code so we can see what changes were made.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    The issue was a for loop I had earlier in the program. I had it going backwards, but for some reason it only read one line and then stopped.
    I also had commented out a method so I could debug, but I forgot to uncomment it.

IMN logo majestic logo threadwatch logo seochat tools logo