Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Program not printing anything

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Program not printing anything

    So I've gotten help already with this program, but I can't seem to get it to print. I know I have to call the methods containing the println statements, but I'm not sure how to go about this. Below is my program. It is supposed to read a string, and then print out the letters in the string and how many times that letter appeared in the string. Then, at the end, it should print out the most frequent letter and how many times that letter appeared. Thanks in advance!

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
      }
    }
    public class LetterProfile
    {
      int scoreboard[] = new int [26];
      private int index;
      private int next;
      private int largest =0;
      private int largestindex =0;
     
     public void countChars (String s)
      {
        s.toLowerCase();
        char a = 'a';
        for (int i =0;i < s.length();i++)
        {
          int next = (int)s.charAt(i)-(int) a;
          if ( next<26 && next >= 0) //if the character is a-z
            scoreboard[next]++;
        }
     }
        public void scoreLine (String line)
        {
          int index = line.length();
          for (int j = 0; j<index; j++)
          {
            scoreboard[j]++;
            System.out.println(j + scoreboard[j]);
          }
        }
        public int largestLength()
        {
          int largest = 0;
          int largestindex = 0;
          for(int a = 0; a<26; a++)
          {
            if(scoreboard[a]>largest)
            largest = scoreboard[a];
            largestindex = a;
          }
          return(char)largestindex;
        }
        public int printResults()
        {
          largestLength();
         System.out.println(largestLength());
        }
     }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    Is this the same problem: Java Program - Letter Frequencies - HELP!

    There needs to be calls made in the main() method to the methods in the class that do the printing. See the discussions on the other thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    Yes, that was my thread. But I tried calling the two methods in the driver and nothing I do works. I am brand new to computer programming so I'm sure I'm just doing it wrong.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    tried calling the two methods in the driver
    Where is the driver code with the calls to the methods? I don't see them in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    I deleted them because they weren't working. I don't know where or how to call these methods but this is one of the ways I tried:

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
        scoreLine();
        largestLength();
      }
    }


    --- Update ---

    Wait, I didn't call the largestLength method I called the printResults method.

    I also tried this:

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
        scoreboard.scoreLine();
        scoreboard.printResults();
      }
    }

    and this:

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
        tScan.scoreLine();
        tScan.printResults();
      }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    What is the while loop supposed to do? The program should ask a user a question BEFORE trying to read input from the user.

    You need to review how to create an instance of a class and call its methods.
    See these spots in the tutorial:
    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    The while loop makes the program stop scanning lines once the user has pressed enter twice (or enters a string of length zero).

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    I suggest you comment out the while loop for now and just work with a String you define in the code for testing the methods in the LetterProfile class. Don't try to do two things at the same time. Test the methods in the class first, then worry about getting user input.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    am I getting closer?

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
        System.out.println("Enter lines of text, type two returns in a row to end.");
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
        LetterProfile k = new LetterProfile();
        k.printResults();
      }
    }

    The program is due tonight so sorry if I'm a little frustrated.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    Did you miss my suggestion:
    I suggest you comment out the while loop for now and just work with a String you define in the code for testing the methods in the LetterProfile class. Don't try to do two things at the same time. Test the methods in the class first, then worry about getting user input.

    am I getting closer?
    What prints out when the code in post#9 executes?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    Nothing prints out. Before, it printed out an input box.

    --- Update ---

    When I type something in the interactions box, it says "Static Error: Undefined name 'Hello world!'".

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    Is that message from your IDE? Have you tried executing the program from the command prompt using the java command?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    I don't know how to use the java command. Yes, that message was from my IDE.

    --- Update ---

    I don't understand what you mean in post #8

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    Like this. Only test the class and its methods. Leave getting user input for later:

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
    /*
        System.out.println("Enter lines of text, type two returns in a row to end.");
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
    */
        LetterProfile k = new LetterProfile();
        k.printResults();
      }
    }

    See this for an example of using the java command:
    http://docs.oracle.com/javase/tutori...ava/win32.html
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    I just tried that, it prints out "25", that's it. so I think his code is fine. also I found a few compilation errors

    btw, kbrady, do you go to umass amherst? attend cmpsci 121? with Moll? this is prog 5?

  16. #16
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    Yes I'm in that class and I am not enjoying myself

    --- Update ---

    Nothing is printing for me still... idk what I'm doing wrong this is awful.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    The printResults() method will print something when it is called. Can you copy the full contents of the console window from when you execute the program and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    Enter lines of text, type two returns in a row to end.
    [DrJava Input Box]
    [DrJava Input Box]
    >

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    Did you comment out the section that tries to read from the user?

    Is the program waiting for the user to enter something?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything


  21. #21
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    can you post a bigger pic? I can barely see anything lol

    can you explain scoreLine? I didn't use that for my submission

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Program not printing anything

    Please copy the text and paste it here. Images aren't that helpful.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything


  24. #24
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything

    yeah I can't get a bigger pic

  25. #25
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Program not printing anything

    Please read and re-read Norm's post above.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: December 11th, 2012, 12:28 PM
  2. [SOLVED] Newbie, why is my program printing output twice?
    By VikingCoder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 07:47 PM
  3. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 09:00 AM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. Triangle Printing - Java Program
    By mparthiban in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 18th, 2010, 04:35 AM