Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

string tokenization using Java

you are going to create a programs. you will practice string tokenization in Java.
1. Prompt the user to input a set of grades, each separated by any amount of white space and a single semicolon, with the enter key to indicate that input of the set is completed. Grades can consist of integers (which we consider a score out of 100) or fractions (which we must convert into a score out of 100). Fractions may or may not be enclosed in parentheses. Compute the average of the grades received and output it back to the user. Assume that all input is valid.

Answer:

import java.util.*;

public class assign2_1 {
	public static void main (String[] args){
		
		/*Get the grades from the user*/
		Scanner keyboard = new Scanner (System.in);
		String input = "";
		System.out.print("Enter a set of grades: ");
		input = keyboard.nextLine();
		
		/*Check tokens separated by the ';' character*/
		StringTokenizer tokInput = new StringTokenizer (input, ";");
		
		/*Declaration and initialization*/
		float numFloat = 0, numeratorF = 0, denominatorF = 0;
		float gradeSum = 0;
		String tokString = "", numString, numeratorS, denominatorS;
		String numeratorTok="", denominatorTok = "";
		int tokCount, gradeCount = 0;
		
		/* Decide number of tokens */
		tokCount = tokInput.countTokens();
		
		/*This loop is used to check every token of the inputted grades*/
		for (int count = 0 ; count < tokCount ; count++){
			//Set all strings we need for numbers to empty */
			numString = "";
			numeratorS = "";
			denominatorS = "";
			/*Checks a new token in sequence */
			tokString = tokInput.nextToken();
			
			/*Check if the token is an integer of 1 to 3 digits*/
			String RegExp1 = "( )*[0-9]{1,3}( )*";
			/*Check if the token is a fraction with or without a bracket*/
			String RegExp2 = "( )*[(]?[0-9]{1,}(/)?[0-9]{1,}( )*[)]?( )*";
			
			/*If we have an integer(percent)*/
			if (tokString.matches(RegExp1)  ){
				
				/*Check every character of the grade token and look for a character that is a number*/
				for (int i = 0 ; i < tokString.length() ; i++ ){
					
					if (tokString.substring(i,i+1).matches("[0-9]")){
						numString += tokString.substring(i,i +1);
					}
				}
				
				/*Convert the float string to float number*/
				numFloat = Float.parseFloat(numString);

				/*Check if the grade is less than 100 */
				if (numFloat <= 100){
					gradeCount += 1;
					gradeSum += numFloat;
				}
			}
			
			/*If we have a fraction*/
			else if(tokString.matches(RegExp2)){
				
				/*Make new tokens separated by the '/' character*/
				StringTokenizer fractionToken = new StringTokenizer (tokString,"/");
			
				/*numerator is the token before '/' */
				numeratorTok = fractionToken.nextToken();
				/*denominator is the token before '/' */
				denominatorTok = fractionToken.nextToken();
			
				/*check every character in the numerator token*/
				for (int i = 0 ; i < numeratorTok.length() ; i++ ){
					
					if (numeratorTok.substring(i,i + 1).matches("[0-9]")){
						numeratorS += numeratorTok.substring(i,i + 1);
					}
				}
				/*Convert the float string to float number*/
				numeratorF = Float.parseFloat(numeratorS);
			
				/*check every character in the denominator token*/ 
				for (int i = 0 ; i < denominatorTok.length() ; i++ ){
					if (denominatorTok.substring(i,i + 1).matches("[0-9]"))
						denominatorS += denominatorTok.substring(i,i + 1);
				}
				
				/*Convert the float string to float number*/
				denominatorF = Float.parseFloat(denominatorS);
				/*Convert the fraction to percent*/
				numFloat = (numeratorF/denominatorF)*100;
				
				/*Check if the grade is less than 100 */
				if (numFloat <= 100){				
					/* Compute total valid grades */
					gradeCount += 1;
					/* Compute sum of all grades*/
					gradeSum += numFloat;
				}
			}
			
				
	}
		/*Display the average on the screen*/
		System.out.println("The average is: " + gradeSum/gradeCount);
	}
	
	}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!