Java Program+To count the number of lines in a method excluding comments???
Hi friends
can u plz help me out, the java program is counting the number of lines in a method excluding comments.
The first thing is how to identify a method, then there can be an inner method inside the parent method,
Please friends its urgent
Bye
Sandy
There's no such thing as an inner method in Java. You can either write the code yourself to parse Java source, or maybe something like ANTLR can do it.
Similar Messages
-
Count the number of lines in each file in a directory
Hello,
I would like to count the number of lines for each file in my directory and subdirectories.
I wonder how can I count the lines of the specified files?
Here is the program which is listed the files in the specified directory:
String path = "C:/User/Studies/Pracices/src/Pract1";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
if (listOfFiles.isFile())
files = listOfFiles[i].getName();
System.out.println(files);Ok so one more question.
Actually my path is pointing to one Directory and like here I can just read from one file that the name of the file should be specified in the path is it possible to help me in that part?
File f = new File("C:/User/Studies/Practices/src/Pract1/");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
LineNumberReader ln = new LineNumberReader(br);
int count = 0;
while (ln.readLine()!=null)
count++;
System.out.println("no. of lines in the file = " + count); -
How to count the number of lines dynamically,
In the below code am trying to read the lines which are selected using a check box, also am categorizing the contents depending on the follow up material.
after displaying one category contents am displaying a line
which also counts to a line in the internal table.
My question is how to count the number of lines(ULINE) displayed dynamically.
FORM GET_LINES .
DATA: LV_LINES TYPE I,
LV_TIMES TYPE I,
LV_TABIX TYPE SY-TABIX.
DESCRIBE TABLE IT_REC LINES LV_LINES.
DO LV_LINES TIMES.
LV_TIMES = SY-INDEX .
READ LINE LV_TIMES FIELD VALUE IT_REC-CHECK INTO GV_CHECK.
IF SY-SUBRC EQ 0 AND GV_CHECK IS NOT INITIAL.
LV_TABIX = LV_TIMES.
READ TABLE IT_REC INDEX LV_TABIX INTO GWA_UPDATE.
IF SY-SUBRC EQ 0.
APPEND GWA_UPDATE TO GT_UPDATE.
ENDIF.
ENDIF.
ENDDO.
ENDFORM. " GET_LINESIn the below code am trying to read the lines which are selected using a check box, also am categorizing the contents depending on the follow up material.
after displaying one category contents am displaying a line
which also counts to a line in the internal table.
My question is how to count the number of lines(ULINE) displayed dynamically.
FORM GET_LINES .
DATA: LV_LINES TYPE I,
LV_TIMES TYPE I,
LV_TABIX TYPE SY-TABIX.
DESCRIBE TABLE IT_REC LINES LV_LINES.
DO LV_LINES TIMES.
LV_TIMES = SY-INDEX .
READ LINE LV_TIMES FIELD VALUE IT_REC-CHECK INTO GV_CHECK.
IF SY-SUBRC EQ 0 AND GV_CHECK IS NOT INITIAL.
LV_TABIX = LV_TIMES.
READ TABLE IT_REC INDEX LV_TABIX INTO GWA_UPDATE.
IF SY-SUBRC EQ 0.
APPEND GWA_UPDATE TO GT_UPDATE.
ENDIF.
ENDIF.
ENDDO.
ENDFORM. " GET_LINES
The display function is:
FORM DISPLAY_DATA .
ULINE.
WRITE : /1 SY-VLINE, 'check',
10 SY-VLINE, 'Plant',
20 SY-VLINE, 'Material number',
50 SY-VLINE, 'Follow up material',
70 SY-VLINE, 'Safety stock',
100 SY-VLINE, 'Partc'.
ULINE.
LOOP AT IT_MARC.
MOVE: IT_MARC-WERKS TO IT_REC-WERKS,
IT_MARC-MATNR TO IT_REC-MATNR,
IT_MARC-NFMAT TO IT_REC-NFMAT,
IT_MARC-EISBE TO IT_REC-EISBE,
IT_MARC-PARTC TO IT_REC-PARTC .
APPEND IT_REC.
CLEAR IT_MARC.
ENDLOOP.
DATA: GV_TABIX TYPE SY-TABIX.
LOOP AT IT_REC.
GV_TABIX = SY-TABIX.
READ TABLE GT_TOTAL WITH KEY WERKS = IT_REC-WERKS
NFMAT = IT_REC-NFMAT.
IF SY-SUBRC EQ 0.
IT_REC-PARTC = GT_TOTAL-PARTC.
MODIFY IT_REC INDEX GV_TABIX TRANSPORTING PARTC.
ENDIF.
ENDLOOP.
LOOP AT IT_REC.
WRITE : /1 SY-VLINE, IT_REC-CHECK AS CHECKBOX,
10 SY-VLINE, IT_REC-WERKS,
20 SY-VLINE, IT_REC-MATNR,
50 SY-VLINE, IT_REC-NFMAT,
70 SY-VLINE, IT_REC-EISBE,
100 SY-VLINE, IT_REC-PARTC.
AT END OF NFMAT.
ULINE.
ENDAT.
ENDLOOP.
ULINE.
ENDFORM. " DISPLAY_DATA
Solved -
Count the number of lines in a txt file
I need to count the number of lines in a txt file, but I can't do it using readLine(). This is because the txt file is double spaced. readLine() returns null even if it is not the end of the file. thanks for the help
I need to count the number of lines in a txt file,
but I can't do it using readLine(). Then just compare each single byte or char to the newline (code 10).
This is because the txt file is double spaced. readLine() returns
null even if it is not the end of the file.Errm what? What do you mean by "double spaced"? Method readLine() should only return null if there's nothing more to read. -
Hi All,
I want to count the Number of lines that the user had entered in a multi-lined text box.
Because I want to restrict him up-to 6 lines.
How Can I don this.
Thanks in Advance,sql>select length('count no
of line
in the text')-length(replace('count no
of line
in the text',chr(10),'')) lines
from dual;
LINES
2
jeneesh -
Count the number of Lines with in a method
I am trying to write a program that will report back the number of methods/types/names and proxies as well as the number of lines of code in each method... I can not figure out how to count each methods line of code separately thanks. each method is commented by
/* PROXY proxyname type */
/* METHOD method name*/
public class Assignment3
private static BufferedReader userInput = new BufferedReader (new InputStreamReader (System.in));
* @param args
* @throws IOException
* @param args
* @throws IOException
public static void main(String[] args) throws IOException
System.out.print("Enter the .java file name: ");
String input = userInput.readLine();
while (!input.equalsIgnoreCase("stop"))
System.out.println("Total Number of Lines: " + LOC(input) + "\n" + Method(input) + "\n" + Type(input));
System.out.print("\nEnter file name to search again or \"stop\" to terminate the program: ");
input = userInput.readLine();
public static int LOC(String inputFile) throws IOException, FileNotFoundException
int numLines = 0;
try
FileReader FileIn = new FileReader(inputFile);
Scanner scanLines = new Scanner (FileIn);
String line;
while (scanLines.hasNext())
line = scanLines.nextLine();
if (line.startsWith("/*"))
numLines--;
else if(!line.equals(" "))
numLines++;
FileIn.close();
catch(IOException IOE)
System.out.println("\nFile Not Found!");
return numLines;
public static int MethodLOC(String inputFile) throws IOException, FileNotFoundException
File FileIn = new File(inputFile);
Scanner scanLines = new Scanner (FileIn);
String line = null;
int methodLOC = 0;
while(scanLines.hasNext())
line = scanLines.nextLine();
if(!line.contains("/*"))
StringTokenizer st = new StringTokenizer(line, "\n");
methodLOC = st.countTokens();
return methodLOC;
public static String Method(String inputFile) throws IOException, FileNotFoundException
String line = null;
String name = null;
String info = null;
File FileIn = new File(inputFile);
Scanner scanLines = new Scanner (FileIn);
while(scanLines.hasNext())
line = scanLines.nextLine();
if(line.contains("METHOD"))
StringTokenizer st = new StringTokenizer(line, " ");
String proxy = st.nextToken();
String method = st.nextToken();
name = st.nextToken();
info = "Name of Method: " + name;
return info;
public static String Type(String inputFile) throws IOException, FileNotFoundException
String line = null;
String name = null;
String info = null;
String count = null;
File FileIn = new File(inputFile);
Scanner scanLines = new Scanner (FileIn);
while(scanLines.hasNext())
if(line.contains("PROXY"))
StringTokenizer st = new StringTokenizer(line, " ");
String proxy = st.nextToken();
String method = st.nextToken();
name = st.nextToken();
String type = st.nextToken();
info = "Name of Proxy: " + name + "\n" + "Type of Proxy: " + type;
count = info;
return count;
}Iterate through the lines and count the opening and closing curly braces. Whenever you see an opening one, increase the counter, when you see a closing one, decrease the counter. When the counter hits 0 again, you reached the end of the method.
I assume you know how to count lines while moving through a text line by line. -
Counting the number of lines for each procedure in a package
Hi,
I would like to write a query on USER_SOURCE that can display the number of code lines for each procedure/function in a package.
Is it possible to write such a query? Maybe by using analytical functions?
for example in the following example i would like to count the lines between "PROCEDURE proc1 IS" and "END proc1;" and between "PROCEDURE proc2 IS" and "END proc2;"
SQL> select text from user_source where name='PKG_TEST' and type='PACKAGE BODY';
TEXT
PACKAGE BODY PKG_TEST IS
PROCEDURE proc1 IS
BEGIN
update t1 set EDITION_NAME = 'AAAAAAA';
commit;
END proc1;
PROCEDURE proc2 IS
BEGIN
update t1 set EDITION_NAME = 'AAAAAAA';
commit;
END proc2;
END PKG_TEST;thanks for helpingonedbguru wrote:
Hopefully some idiot manager isn't trying to use this sort of thing to base their decisions on developer productivity. If so, they ARE idiots. Number of code lines NEVER, EVER, EVER!!! translates into an efficient application. If I were paid by the line, my code would look like:
select
1
from
dual
Based on the idiocy of this method of determining "performance" I would get paid $5.00 at $1.00/line. And trust me, I saw this back in the 80's and 90's and saw similar idiotic code and had to clean up the mess. Most of it wasn't efficient code to start with let alone the number of lines generated.Or the other extreme, when the PHB firmly believed that execution efficiency was gained by reducing the number of lines of code. So instead of nicely formatted, easy to read code:
*GET NEXT ORDER NUMBER
EXEC SQL
SELECT (MAX(ORDER_NUM) + 1)
INTO :NEXT-NUM:IND-NULL
FROM PART_ORDER
END-EXEC.
IF IND-NULL < 0
MOVE 1 TO NEXT-NUM.
EXEC SQL
INSERT
INTO PART_ORDER
(ORDER_NUM,
ORIGIN_LOC,
ORDER_TYPE,
ORDER_STAT,
CREAT_TIME)
VALUES (:NEXT-NUM,
:LOC, 'R', 'O',
CURRENT TIMESTAMP)
END-EXEC.
MOVE NEXT-NUM TO MASK0.
PERFORM HEADER-PROC THRU HEADER-EXIT.
CREATE-ORDER-EXIT. EXIT. You got this:
*GET NEXT ORDER NUMBER
EXEC SQL SELECT (MAX(ORDER_NUM) + 1) INTO :NEXT-NUM:IND-NULL FROM
PART_ORDER END-EXEC. IF IND-NULL < 0 MOVE 1 TO NEXT-NUM. EXEC SQL INSERT
INTO PART_ORDER (ORDER_NUM, ORIGIN_LOC, ORDER_TYPE, ORDER_STAT,
CREAT_TIME) VALUES (:NEXT-NUM, :LOC, 'R', 'O', CURRENT TIMESTAMP)
END-EXEC. MOVE NEXT-NUM TO MASK0. PERFORM HEADER-PROC THRU HEADER-EXIT.
CREATE-ORDER-EXIT. EXIT. Oh, wait! See the same thing all the time in this forum ... not because someone thinks fewer lines = faster code, but simply because people are sloppy.
INSERT INTO PART_ORDER(ORDER_NUM,ORIGIN_LOC,ORDER_TYPE,ORDER_STAT,CREAT_TIME) VALUES (:NEXT-NUM,:LOC, 'R', 'O',CURRENT TIMESTAMP) ; -
Count the number of lines in all of my java files.
I have created a reasonable size project with about 20 classes, rather than going through each and counting the lines are their any programs maby integrated into forte or another ide which can do this?
ThanksActually, if you are looking for kloc statitics, it is more complicated. You would not want blank lines counted. You may also want to separate out the counts for comments vs. code. Also seprations between JavaDoc comments and commented-out code. There are free and commercial programs which do this, or you can write your own.
-
Have EEM count the number of Lines
In *nix this is as simple as "wc -l" but unfortunately I can't seem to find a simple solution. For the purposes of this thread here is my test script...
event manager applet TEST authorization bypass
event syslog pattern "TRIGGER"
action 1.0 cli command "enable"
action 2.0 cli command "conf t"
action 3.0 cli command "show log"
action 4.0 cli command "COUNT NUMBER OF LINES FROM SHOW LOG -- $_cli_result"
Can someone let me know what action 4.0 would be in order to complete this task?
Thanks!Actually, that parially worked. Perhaps you can help me troubleshoot the results I am getting.
Here is my test script:
edge2821a#show run | sec eventevent manager applet TEST authorization bypass event none action 1.0 cli command "enable" action 2.0 cli command "conf t" action 3.0 cli command "show ip nat trans" action 4.0 set LINES "0" action 4.1 foreach line "$_cli_result" "\n" action 4.2 increment LINES action 4.3 end action 4.4 puts "Number of NAT Translations: $LINES"
Here is my actual NAT translation table:
edge2821a#show ip nat translationsPro Inside global Inside local Outside local Outside globaludp 72.68.68.238:123 10.0.10.200:123 66.27.60.10:123 66.27.60.10:123tcp 72.68.68.238:4030 10.0.20.100:4030 --- ---tcp 72.68.68.238:4031 10.0.20.100:4031 --- ---tcp 72.68.68.238:23 10.0.20.105:22 --- ---tcp 72.68.68.238:80 10.0.20.105:80 199.248.199.110:59691 199.248.199.110:59691tcp 72.68.68.238:80 10.0.20.105:80 --- ---tcp 72.68.68.238:9090 10.0.20.201:8080 --- ---tcp 72.68.68.238:443 10.0.20.225:80 --- ---tcp 72.68.68.238:9022 10.0.21.230:22 173.70.201.69:49353 173.70.201.69:49353tcp 72.68.68.238:9022 10.0.21.230:22 --- ---udp 72.68.68.238:50817 10.0.21.231:50817 63.142.161.5:3997 63.142.161.5:3997udp 72.68.68.238:55703 10.0.21.231:55703 63.142.161.5:1194 63.142.161.5:1194udp 72.68.68.238:49917 10.0.30.30:49917 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:50011 10.0.30.30:50011 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:51973 10.0.30.30:51973 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:56512 10.0.30.30:56512 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:56639 10.0.30.30:56639 71.250.0.12:53 71.250.0.12:53tcp 72.68.68.238:57373 10.0.30.30:57373 64.12.24.171:443 64.12.24.171:443tcp 72.68.68.238:57404 10.0.30.30:57404 173.194.73.125:5222 173.194.73.125:5222Pro Inside global Inside local Outside local Outside globaltcp 72.68.68.238:57412 10.0.30.30:57412 173.194.68.109:465 173.194.68.109:465tcp 72.68.68.238:57413 10.0.30.30:57413 173.194.68.109:587 173.194.68.109:587tcp 72.68.68.238:57414 10.0.30.30:57414 173.194.68.109:465 173.194.68.109:465udp 72.68.68.238:57572 10.0.30.30:57572 71.250.0.12:53 71.250.0.12:53tcp 72.68.68.238:60348 10.0.30.30:60348 173.194.76.108:993 173.194.76.108:993udp 72.68.68.238:60413 10.0.30.30:60413 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:61461 10.0.30.30:61461 71.250.0.12:53 71.250.0.12:53tcp 72.68.68.238:61632 10.0.30.30:61632 17.149.36.76:5223 17.149.36.76:5223udp 72.68.68.238:62019 10.0.30.30:62019 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:63756 10.0.30.30:63756 71.250.0.12:53 71.250.0.12:53udp 72.68.68.238:65134 10.0.30.30:65134 71.250.0.12:53 71.250.0.12:53tcp 72.68.68.238:65188 10.0.30.30:65188 173.194.68.109:993 173.194.68.109:993tcp 72.68.68.238:65274 10.0.30.30:65274 173.194.74.108:993 173.194.74.108:993tcp 72.68.68.238:65310 10.0.30.30:65310 173.194.76.108:993 173.194.76.108:993tcp 72.68.68.238:65311 10.0.30.30:65311 173.194.68.108:993 173.194.68.108:993tcp 72.68.68.238:65312 10.0.30.30:65312 173.194.68.108:993 173.194.68.108:993tcp 72.68.68.238:65374 10.0.30.30:65374 173.194.76.108:993 173.194.76.108:993tcp 72.68.68.238:65429 10.0.30.30:65429 173.194.74.109:993 173.194.74.109:993tcp 72.68.68.238:65466 10.0.30.30:65466 74.125.226.195:80 74.125.226.195:80tcp 72.68.68.238:65467 10.0.30.30:65467 74.125.226.200:80 74.125.226.200:80tcp 72.68.68.238:65469 10.0.30.30:65469 173.194.43.7:443 173.194.43.7:443tcp 72.68.68.238:65470 10.0.30.30:65470 173.194.43.7:443 173.194.43.7:443tcp 72.68.68.238:65471 10.0.30.30:65471 173.194.43.7:443 173.194.43.7:443Pro Inside global Inside local Outside local Outside globaltcp 72.68.68.238:65472 10.0.30.30:65472 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65473 10.0.30.30:65473 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65474 10.0.30.30:65474 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65475 10.0.30.30:65475 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65476 10.0.30.30:65476 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65477 10.0.30.30:65477 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65478 10.0.30.30:65478 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65479 10.0.30.30:65479 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65480 10.0.30.30:65480 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65481 10.0.30.30:65481 173.194.75.99:443 173.194.75.99:443tcp 72.68.68.238:65482 10.0.30.30:65482 192.168.1.1:2555 192.168.1.1:2555tcp 72.68.68.238:65484 10.0.30.30:65484 206.46.232.12:465 206.46.232.12:465tcp 72.68.68.238:65485 10.0.30.30:65485 173.194.68.109:465 173.194.68.109:465tcp 72.68.68.238:65486 10.0.30.30:65486 173.194.68.109:465 173.194.68.109:465tcp 72.68.68.238:65487 10.0.30.30:65487 173.194.68.109:587 173.194.68.109:587tcp 72.68.68.238:52115 10.0.30.60:52115 204.8.241.230:443 204.8.241.230:443tcp 72.68.68.238:52523 10.0.30.60:52523 204.8.241.230:443 204.8.241.230:443tcp 72.68.68.238:52524 10.0.30.60:52524 204.8.241.230:443 204.8.241.230:443tcp 72.68.68.238:52525 10.0.30.60:52525 204.8.241.230:443 204.8.241.230:443tcp 72.68.68.238:52571 10.0.30.60:52571 17.172.232.217:5223 17.172.232.217:5223tcp 72.68.68.238:52861 10.0.30.60:52861 173.194.74.108:993 173.194.74.108:993tcp 72.68.68.238:53667 10.0.30.60:53667 204.8.241.230:443 204.8.241.230:443Pro Inside global Inside local Outside local Outside globaltcp 72.68.68.238:53668 10.0.30.60:53668 173.194.68.109:993 173.194.68.109:993tcp 72.68.68.238:50519 10.0.30.62:50519 69.171.230.22:443 69.171.230.22:443tcp 72.68.68.238:50597 10.0.30.62:50597 17.149.36.120:443 17.149.36.120:443tcp 72.68.68.238:50889 10.0.30.62:50889 173.252.103.48:443 173.252.103.48:443tcp 72.68.68.238:51959 10.0.30.62:51959 23.67.251.64:80 23.67.251.64:80tcp 72.68.68.238:52085 10.0.30.62:52085 69.171.233.33:443 69.171.233.33:443tcp 72.68.68.238:52091 10.0.30.62:52091 17.149.36.162:443 17.149.36.162:443tcp 72.68.68.238:52216 10.0.30.62:52216 17.172.233.111:443 17.172.233.111:443tcp 72.68.68.238:53102 10.0.30.206:53102 184.51.206.137:80 184.51.206.137:80tcp 72.68.68.238:52958 10.0.30.233:52958 173.194.68.108:993 173.194.68.108:993tcp 72.68.68.238:52959 10.0.30.233:52959 173.194.68.109:993 173.194.68.109:993tcp 72.68.68.238:52961 10.0.30.233:52961 173.194.68.108:993 173.194.68.108:993tcp 72.68.68.238:52962 10.0.30.233:52962 173.194.68.108:993 173.194.68.108:993tcp 72.68.68.238:59069 10.0.30.233:59069 17.158.10.36:443 17.158.10.36:443tcp 72.68.68.238:59070 10.0.30.233:59070 17.158.10.36:443 17.158.10.36:443tcp 72.68.68.238:59072 10.0.30.233:59072 74.125.226.233:443 74.125.226.233:443tcp 72.68.68.238:65140 10.0.30.233:65140 173.194.73.125:5222 173.194.73.125:5222tcp 72.68.68.238:53041 10.0.30.234:53041 173.194.68.108:993 173.194.68.108:993tcp 72.68.68.238:53250 10.0.30.234:53250 173.194.74.108:993 173.194.74.108:993tcp 72.68.68.238:53520 10.0.30.234:53520 17.172.232.158:443 17.172.232.158:443tcp 72.68.68.238:54172 10.0.30.235:54172 17.149.36.165:5223 17.149.36.165:5223tcp 72.68.68.238:54173 10.0.30.235:54173 17.172.238.204:443 17.172.238.204:443Pro Inside global Inside local Outside local Outside globaltcp 72.68.68.238:55523 10.0.30.235:55523 173.194.76.108:993 173.194.76.108:993
Here is the output of my EEM script:
edge2821a#event manager run TESTNumber of NAT Translations: 4
The value of $LINES shoudl have been 92, not 4. Can you help me to identify the problem?
Thanks! -
How can i read the number of lines written - in spreadshee​t array?
hello NI,
I am writing file as spreadsheet format continuously, contains date, time, chanel1, channel 2, channel 3..... etc.
i just want to know how many lines has been written after file write....
Regards,
Balaji DPYou count the number of line termination char. Se example
Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
Attachments:
sample.PNG 25 KB -
Hi all,
can you please tell me how to get the number of lines in each method in a java file.
class test
public void method(){
sop()...
sop()...
In that above class, the number of lines in "method" is 2.
is there any api in java which will return us the number of lines in each method in a java file?
Please suggest me..-I was wondering about some crazy bytecode reader.javap?
-I agree it's not the best solution, but going for 100% accuracy wasn't the aim here :)Please understand that I'm not criticizing your attempt, but rather that the OP's requirements are far too vague to provide any useful advice.
-How does it not deal with loops/code blocks? (case statements maybe as I forgot ":")Example:
String method =
"void method() {\n" +
" for (int i = 0; i < 1; i++) {\n" +
" System.out.println(this.toString());\n" +
" }\n" +
int expectedLineCount = 3; // possibly, based on what the OP's indicated so far...
int actualLineCount = method.split("[;{}]").length; // per your example
assert expectedLineCount != actualLineCount;~ -
Help!! count the number of words in one line
the question is that use JOptionPane and Array to count the number of words and characters that user inputed.
for example, if I enter the " this is a java program"
that messages have display 5 and 18.
please show me the a completely program.
thx!!!!You guys are heartless. Even you weren't born with programming knowledge hard-coded into your brain. Even you had to start from zero. Even you had to struggle at something in your life. In this spirit, I think that we should give this poor student a break and try to help him as much as possible. Here, try out my program, and perhaps it will give you some ideas for your own:
public class WordCountingHomework
public static void main(String[] args) throws InterruptedException
String input = JOptionPane.showInputDialog("Please enter a String");
// get your String and split the String into words
// This will allow you to count words easily
String[] strArray = new String(wordCountByteArray).split(" ");
int delay = 400;
for (;;)
// loop through the array to count the words
for (String string : strArray)
System.out.print(string + " ");
Thread.sleep(delay);
System.out.println();
delay *= 7;
delay /= 10;
private static byte[] wordCountByteArray =
0x50, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x6f,
0x75, 0x72, 0x20, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x61, 0x72, 0x6b, 0x69,
0x6e, 0x27, 0x20, 0x68, 0x6f, 0x6d, 0x65, 0x77, 0x6f, 0x72, 0x6b, 0x21
} -
Help with how to count the number of tokens from the first line
Hi
I want to count the number of tokens from the first line of text, from my incoming file and print it out.
For example i have 12 tokens in the first line, so i want it to print out the number 12
my code is below
thanks
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;
import java.sql.*;
public class Data {
public static void main(String[] args) {
String token = null;
String currentToken = "";
Hashtable hh = new Hashtable();
try {
Vector v2 = new Vector(100);
Vector v = new Vector(100);
FileReader file = new FileReader("words.txt");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
StringTokenizer s = null;
boolean eof = false;
while (!eof){
s = new StringTokenizer(line.trim(),",",true);
boolean lastToken = true;
while (s.hasMoreTokens()) {
token = s.nextToken();
if (lastToken){
if (token.equals(",")) {
v.add("");
lastToken = false;
if (! token.equals(",")) {
v.add(token);
else { if (currentToken.equals(",")) {
v.add("");
currentToken = token;
if (token.equals(",")) {
v.add("");
line = buff.readLine();
if (line == null){
eof = true;}
buff.close();
}catch (FileNotFoundException fe) {
System.out.println("Error - - " + fe.toString());
}catch (NumberFormatException ne) {
System.out.println("Error - - " + ne.toString());
} catch (IOException e) {
System.out.println("Error - - " + e.toString());
} 2.I can print out the amount of tokens from each line,
But i only want the first line to be counted, And i
also do not want the delimeters to be counted.
could you please give me some ideas?
Thanks
try
BufferedReader br = new BufferedReader(new FileReader("item.kr"));
while((str=br.readLine())!= null)
itemslist.add(str);
catch(IOException ioe)
System.out.println("Exception Occurred!!!");
}This snippet gets one Line from the item.kr file.
Then i would count (By useing a diffrent String Tokenizer) to count only this line!!! -
Java Comiler - How to find out the Number of lines of Executable code
Hello,
Is there any option we have or could have to get the "Number of Lines of Executable Code" while we run the java compiler?
Technically Java is an interpreter. So in case the term is incorrect, please excuse me.
Since the compiler anyway traverses through the Java files, it is easy and correct to get the number from the compiler rather than having any external tool calculating the same.
It should remove the comments, blank lines and take care of java single sentence written in multiple lines.
Any suggestions or help is highly appreciated.
regards,
SabyasachiHow many lines of code are there in this program?
public class sabre20110211 {public static void main(String[] args) {for (int i = 0; i < 10; i++)System.out.println(i);}}And how many in this program?
public
class
sabre20110211
public
static
void
main(
String[]
args
for
(int i = 0;
i < 10;
i++
System.out.println(i);
} -
How to Count the number of TAB chars value '0A' in a line?
I have a requirement to count the number of TABs in a text file that I am processing (they must equal 75) to ensure that all the fields have been submitted on the file I am reading?
How do I identify the TABs in a line?
I have this so far:
CONCATENATE gv_unix_file gv_lstfiles-name INTO gv_unix_file.
OPEN DATASET gv_unix_file for INPUT IN TEXT MODE ENCODING NON-UNICODE.
IF sy-subrc <> 0 .
message e000(zz) with 'Cannot access in_uk files'.
ENDIF.
CONCATENATE gv_unix_file_arc gv_lstfiles-name INTO gv_unix_file_arc.
OPEN DATASET gv_unix_file_arc FOR OUTPUT IN TEXT MODE ENCODING NON-UNICODE.
IF sy-subrc <> 0 .
message e000(zz) with 'Cannot access in_uk files'.
ENDIF.
DO.
READ DATASET gv_unix_file into lv_str.
IF sy-subrc <> 0.
exit. "exit do loop, file is done.
else.
TRANSFER lv_str to gv_unix_file_arc.
ENDIF.
ENDDO.Here is a sample code:
DATA: lv_string TYPE string,
lv_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
lv_i TYPE i.
DATA: result_tab TYPE match_result_tab.
DO 3 TIMES.
CONCATENATE 'A' lv_string INTO lv_string SEPARATED BY lv_tab.
ENDDO.
" this is how you need to find how many tabs are in your transfer work area...
FIND ALL OCCURRENCES OF lv_tab IN lv_string RESULTS result_tab.
DESCRIBE TABLE result_tab LINES lv_i.
WRITE:'No. of tabs found:', lv_i.
Maybe you are looking for
-
TLF in pure AS3 project?? Big swc woes...
I'm trying to find any info on using TLF text in a pure AS project (using FDT), but quickly finding myself getting confused by it all. My normal workflow when laying out text is to - use Flash Pro to lay my text out within a MovieClip, - set that Mov
-
Hi folks. I am tasked to put together a purchase order request form and it has to be in PDF format. I originally built it in Excel, and after a day and a half of googling, I finally came upon the formula(s) that make 2 of columns interact the way I w
-
DateTimeConnect in UCM Call Detail Report
An example of a dateTimeConnect value in UCM CDR: 1289328321 What is that? How would I convert this to mm/dd/yy:hh:mm::ss? Thank you.
-
Viewing flash based web content
Hello, I am curious... On my mac book I am being asked to install flash for certain websites with embedded video, primarily my college intranet, but also BBC News for example. Yet I can view these videos on my iPad and I know flash is not installed.
-
Can Firefox open a new tab when I click on a bookmark?
Can I get Firefox 5.01 to open bookmarks in a new tab when I click on them?