Results 1 to 3 of 3
Thread: Part of program not printing ?
- 04-08-2009, 05:48 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
Part of program not printing ?
The following program reads input from a .txt file and prints out the text all in lowercase and then prints out the frequency of the letters used. The problem that I am having is I can get my "try" block to work when the "for loop" is not there and same with the "for loop"...works when I remove the "try" block ? Program does what I want it to do , I just can not get the everything to work as one ? Thank you for any suggestions in advance.
Java Code:import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; import javax.swing.JOptionPane; public class SampleTest { public static void main(String arg[]) throws FileNotFoundException, IOException { String ans; ans = JOptionPane.showInputDialog(null, "Please enter in the location of your file "); BufferedReader br = new BufferedReader(new FileReader(ans)); Scanner sc = new Scanner(br).useDelimiter(" "); String str = sc.nextLine().toLowerCase(); char[] letters = new char[250]; br.read(letters); char[] c=str.toCharArray(); int loopcount=0; int count=0; while(sc.hasNext()){ try{ for (int i = 0; i < letters.length; i++) { char ch = letters[i]; String d = Character.toString(ch).toLowerCase(); System.out.print(d); } } catch(Exception exception) { System.out.println("You did nto enter a correct file format."); } // String d = Character.toString(c).toLowerCase(); } System.out.println(""); System.out.println("Letter Frequency"); for(int i=0;i<c.length;i++) { boolean flag=true; for(int k=0;k<i;k++){ if(c[i]==(str.charAt(k))) flag=false; } if(flag){ for(int j=0;j<str.length();j++) { if(c[i]==str.charAt(j)) count=count+1; } System.out.println(c[i]+" "+" "+(count)); count=0; loopcount++; } } } }
- 04-08-2009, 11:08 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
while(sc.hasNext()){
// code
}
sc.hasNext() return true, then print that 250 char
sc.hasNext() return true again, then print the same 250 char
so on...
do you find the problem?
problem is not the try block, is the while loop
text .....xx(250 char) sdfa bbcc....
you use scanner to read first 250 char, then ask scanner, still have chars?, scanner said yes,
you do something... then ask scanner again...
scanner remain at xx positionLast edited by mtyoung; 04-08-2009 at 11:21 AM.
- 04-09-2009, 03:37 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
Ok, I have reworked the code. Problem I am having is that I can not find enough info anywhere on nested "try" blocks. So again when I run each loop on it own everything works. When I try to nest them in a try block , I will get the second loop to kind of work but never the first. Can anyone see the issue that i am having and provide me with some insight to my incorrect nesting of the try block. Thank you.
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; import javax.swing.JOptionPane; public class sConvert { public static void main(String[] args) throws IOException { String ans; ans = JOptionPane.showInputDialog(null, "Please enter in the location of your file "); BufferedReader br = new BufferedReader(new FileReader(ans)); Scanner sc = new Scanner(br).useDelimiter(" "); String str = sc.nextLine().toLowerCase(); char[] c=str.toCharArray(); int loopcount=0; int count=0; char[] letters = new char[250]; br.read(letters); for (int i = 0; i < letters.length; i++) try{ { char ch = letters[i]; String d = Character.toString(ch).toLowerCase(); System.out.println(d); } for(int j=0;j<c.length;j++) try{ //for(int j=0;j<c.length;j++) { boolean flag=true; for(int k=0;k<i;k++){ if(c[j]==(str.charAt(k))) flag=false; } if(flag){ for(int l=0;l<str.length();l++) { if(c[j]==str.charAt(l)) count=count+1; } System.out.println(c[j]+" "+" "+(count)); count=0; loopcount++; } } } catch(Exception ioe) { System.out.println("IOException " + ioe); } } catch(Exception ioe) { System.out.println("IOException " + ioe); } }
Similar Threads
-
Inventory Program Part 3 ~ please help!
By marMcD in forum New To JavaReplies: 13Last Post: 02-25-2009, 05:57 AM -
Java Inventory Program Part 3
By ljk8950 in forum New To JavaReplies: 18Last Post: 07-28-2008, 05:47 AM -
Inventory Program Part 3 - DUE TODAY (7/28/08)
By ljk8950 in forum New To JavaReplies: 7Last Post: 07-27-2008, 10:28 PM -
A Fibonacci printing program
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 07:26 PM -
Inventory part 3 program problems
By badness in forum New To JavaReplies: 1Last Post: 12-17-2007, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks