Question

Write a program that reads a Java source-code file and reports the number of keywords (including...

Write a program that reads a Java source-code file and reports the number of keywords (including null, true, and false) in the file. If a keyword is in a comment or in a string, don’t count it. Test your program to count the keywords in the Welcome.java file:

Please input the Java filename: Welcome.java
The number of keywords in the program is 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package numberofkeywords;

/**

*

* @author miracle

*/

//Write a program that reads a Java source-code file and reports the number of keywords

//(including null, true, and false) in the file. If a keyword is in a comment or in a string,

//don’t cnt it.

import java.util.*;

import java.io.*;

public class NumberOfKeywords {

public static void main(String[] args) throws Exception {

Scanner sc = new Scanner(System.in);

System.out.print("Please input the Java file name: ");

String file_name = sc.nextLine();

File file = new File(file_name);

if (file.exists() && file_name.substring(

file_name.lastIndexOf(".")).equals(".java")) {

System.out.println("The number of keywords in the program is " + file_name

+ " is " + no_of_keywords(file));

}

else {

System.out.println("File " + file_name + " not found");

}

}

//reports the number of keywords

//(including null, true, and false) in the file.

public static int no_of_keywords(File file) throws Exception {

// Array of all Java Keywords + ture, false and null

String[] keywordString = {"abstract", "assert", "boolean",

"break", "byte", "case", "catch", "char", "class", "const",

"continue", "default", "do", "double", "else", "enum",

"extends", "for", "final", "finally", "float", "goto",

"if", "implements", "import", "instanceof", "int",

"interface", "long", "native", "new", "package", "private",

"protected", "public", "return", "short", "static",

"strictfp", "super", "switch", "synchronized", "this",

"throw", "throws", "transient", "try", "void", "volatile",

"while", "true", "false", "null"};

Set<String> keywordSet =

new HashSet<>(Arrays.asList(keywordString));

int cnt = 0;

Scanner sc = new Scanner(file);

while (sc.hasNext()) {

String word = sc.next();

//If a keyword is in a comment or in a string,

//don’t cnt it.

if (word.equals("//"))

sc.nextLine();

else if (word.contains("\""))

while (sc.hasNext() && !sc.next().contains("\"")) {}

else if (word.contains("/*"))

while (sc.hasNext() && !sc.next().contains("*/")) {}

else if (keywordSet.contains(word))

cnt++;

}

return cnt;

}

}

Add a comment
Know the answer?
Add Answer to:
Write a program that reads a Java source-code file and reports the number of keywords (including...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Count the occurrences of each keyword) Write a python program that reads in a Python source...

    (Count the occurrences of each keyword) Write a python program that reads in a Python source code file and counts the occurrence of each keyword in the file. Your program should prompt the user to enter the Python source code filename.

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Write a program in Java that reads the file and does two things: Write to an...

    Write a program in Java that reads the file and does two things: Write to an output file called dataSwitch.txt the same data from the input file, but switch the order of the data entries on each line. For example, if the first line of the input file is “90001 57110”, the first line of the output file would be “57110 90001”. Additionally, print to the screen the number of lines in the file with a label

  • This needs to be linux compatible in C++ code. Write a program that reads a file...

    This needs to be linux compatible in C++ code. Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167,...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT