Some weird interactions are happening in my code, but I can't find a solution

PGkiokas :

My code for calculating average is correct but my program wont stop executing they way I want it to. I have to use scanner's functions in order to manipulate the string.

So I wanted to get better at Java and my tutor gave me a problem to solve. The program must accept student id, name of the subject and its marks, all in one string, and then calculate the average marks for this id. The format of the sting is "123456 math 5.5 physics 6.5 end" where the 6 digit number is the id and "end" is what makes the program stop waiting for numbers and calculate them.

  • ID is a 6 digit number from 0 to 9 which if it is 000000 the program must terminate.

  • "End" is stopping the entry but not the loop, but "000000" ends the loop.

In my code everything works fine but in order for the variables I use to reset I must type two times the word "end" and if I want to stop the loop I must type "000000" twice which is clearly not what I want. Can you point out my mistakes?

My teacher suggested to use Scanner's functions in order to get what I want. Is this problem something I cannot solve because of they way these functions work?

package averagemarks;
import java.util.*;

class AverageMarks {
public static void main(String args[]) {
    String s = "123456 pr1 6.2 pr2 7.3";   //string input format
    Scanner sc = new Scanner(System.in); 
    sc.useDelimiter("\\s+");               //space to split string tokens
    sc.useLocale(Locale.US);               //with that it can recognise float numbers

    int count = 0;
    double sum = 0.0;
    String AM;                              //6 numbers from 0-9 consists the student id 
    System.out.println("Give string: ");

    while(sc.hasNext()) {                       
        String str = sc.next();

            if (sc.hasNext("[0-9]{6}")) {                   //this is where i check for the id which if it is 000000 it must terminate the loop
                AM=str;
                System.out.println("AM: " + AM);
                if (AM.equals("000000")) break;
            } else if(sc.hasNext()== false) {                       //string must end with an end if user does not end it with it i must inform him
                System.out.println("Data format error.");
                //return;
            } else if(sc.hasNextFloat()){                             //this is where i gather data for the result
                sum += sc.nextFloat();
                count++;                                
            } else if(sc.hasNext("end")){                             //end means that you gonna calculate all the numbers till that point then reset for another student
                System.out.println("Average is " + sum / count);
                count = 0;
                sum = 0.0;
            }
        }
    }
}
SirFartALot :

"In my code everything works fine"

Well, it does not!

With the line String str = sc.next(); you read away required data.

You should put it in the end to read the course name away.

while(sc.hasNext()) {                       
    // check student id
    if (sc.hasNext("[0-9]{6}")) {                 
        AM=sc.next(); // read student number
        System.out.println("AM: " + AM);
        if (AM.equals("000000")) break;
    } 
    // check if line ends prematurely --> error
    else if(sc.hasNext()== false) {
        System.out.println("Data format error.");
        //return;
    } 
    // check for a float
    else if(sc.hasNextFloat()){                  
        sum += sc.nextFloat();  // read the float
        count++;                                
    } 
    // check for "end"
    else if(sc.hasNext("end")){
        String endStr = sc.next(); // read "end"
        System.out.println("Average is " + sum / count);
        count = 0;
        sum = 0.0;
    } 
    // all other not yet considered strings (e.g. a course)
    else {        
        String course = sc.next();  // read course
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157067&siteId=1