Results 1 to 3 of 3
  1. #1
    graz is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default 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++;
    }
    }
    }
    
    
      
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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 position
    Last edited by mtyoung; 04-08-2009 at 12:21 PM.

  3. #3
    graz is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    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

  1. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 06:57 AM
  2. Java Inventory Program Part 3
    By ljk8950 in forum New To Java
    Replies: 18
    Last Post: 07-28-2008, 06:47 AM
  3. Inventory Program Part 3 - DUE TODAY (7/28/08)
    By ljk8950 in forum New To Java
    Replies: 7
    Last Post: 07-27-2008, 11:28 PM
  4. A Fibonacci printing program
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:26 PM
  5. Inventory part 3 program problems
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-17-2007, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •