Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams
this week in the
Agile and Other Processes
forum!
Avi Chat
Greenhorn
+ Follow
2
Posts
1
Threads
0
Cows
since Oct 28, 2009
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (2/100)
Number Threads Started (1/100)
Number Cows Received (0/5)
Number Likes Received (0/10)
Number Likes Granted (0/20)
Set bumper stickers in profile (0/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (2/10)
Number Threads Started (1/10)
Number Likes Received (0/3)
Number Likes Granted (0/3)
Set bumper stickers in profile (0/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Avi Chat
Word count problem.
Thanks for the prompt reply
.
import java.util.*; import java.io.*; public class search2 { public static void main(String[]args) throws IOException { char awnChar, contiChar='y'; String awnser, conti, in, line; int word=0, sentence=0, vowel=0, consonants=0, digit=0, i=0, length = 0, lengthAll = 0; Scanner scan = new Scanner(System.in); //System.out.print("Enter the name of the file: "); //String fileName = scan.nextLine(); //File file = new File(fileName+".txt"); File file = new File("Quotation.txt"); Scanner input = new Scanner(file); while(input.hasNext()) { line = input.nextLine(); length = line.length(); lengthAll = length+lengthAll; } do { System.out.println("What would you like to determine? "); System.out.println("a.the number of words in the file "); System.out.println("b.the number of sentences in the file "); System.out.println("c.the number of vowels in the file "); System.out.println("d.the number of consonants in the file "); System.out.println("e.the number of digits (0 - 9) in the file "); System.out.println(); System.out.print("Input the letter: "); System.out.println(); awnser = scan.nextLine(); awnChar = awnser.charAt(0); if(Character.toLowerCase(awnChar) == 'a') { while(lengthAll>i) { in = input.next(); if (in.charAt(i) == ' ') word++; i++; } System.out.println("There are "+word+" word(s) in that document."); } else if (Character.toLowerCase(awnChar) == 'b') { while(lengthAll>i) { in = input.next(); if (in.charAt(i) == '?' || in.charAt(i) == '!' || in.charAt(i) == '.') sentence++; i++; } System.out.println("There are "+sentence+" sentence(s) in that document."); } i = 0; System.out.println("Would you like to continue? [Y/N] "); conti = scan.nextLine(); contiChar = conti.charAt(0); System.out.println(); }while(Character.toLowerCase(contiChar) == 'y'); } }
Would it be something like that? but i am still getting errors. Still very new at this :S
show more
14 years ago
Beginning Java
Word count problem.
I am having trouble making a java program that reads a document and tells the user how many words there are in the document however it keeps giving me the following error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: 4 at java.lang.String.charAt(String.java:687) at search2.main(search2.java:43) Press any key to continue . . .
this is what i got so far.
import java.util.*; import java.io.*; public class search2 { public static void main(String[]args) throws IOException { char awnChar, contiChar='y'; String awnser, conti, in; int word=0, sentence=0, vowel=0, consonants=0, digit=0, i=0, length = 0; Scanner scan = new Scanner(System.in); //System.out.print("Enter the name of the file: "); //String fileName = scan.nextLine(); //File file = new File(fileName+".txt"); File file = new File("Quotation.txt"); Scanner input = new Scanner(file); do { System.out.println("What would you like to determine? "); System.out.println("a.the number of words in the file "); System.out.println("b.the number of sentences in the file "); System.out.println("c.the number of vowels in the file "); System.out.println("d.the number of consonants in the file "); System.out.println("e.the number of digits (0 - 9) in the file "); System.out.println(); System.out.print("Input the letter: "); System.out.println(); awnser = scan.nextLine(); awnChar = awnser.charAt(0); if(Character.toLowerCase(awnChar) == 'a') { while(input.hasNext()) { in = input.next(); if (in.charAt(i) == ' ') word++; i++; } System.out.println("There are "+word+" word(s) in that document."); } else if (Character.toLowerCase(awnChar) == 'b') { while(input.hasNext()) { in = input.next(); if (in.charAt(i) == '?' || in.charAt(i) == '!' || in.charAt(i) == '.') sentence++; i++; } System.out.println("There are "+sentence+" sentence(s) in that document."); } i = 0; System.out.println("Would you like to continue? [Y/N] "); conti = scan.nextLine(); contiChar = conti.charAt(0); System.out.println(); }while(Character.toLowerCase(contiChar) == 'y'); } }
Any help appreciated. Thanks.
show more
14 years ago
Beginning Java