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.

Results 1 to 15 of 15

Thread: Program not printing anything II

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

    Default Program not printing anything II

    I am in the same situation and need help with the same problem. What you have for code is everything I have had since last week and it still prints "25" Ive tried fixing it many times and nothing had changed.


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Program not printing anything

    It is printing 25 because in your for loop the if statement doesnt have brackets around the lines after it. I recommend always putting brackets around your blocks. You will avoid many of these errors in your future. Your largestindex variable is getting "a" which will always end in 25 because it is being set every time. IF STATEMENTS WITHOUT BRACKETS ONLY CONTROL ONE LINE DIRECTLY AFTER THEM. Use brackets...for the love of god use brackets. The sooner you do this the happier you'll be.

    For those still looking to figure this out, i have the program working in my possession. I will help you work through your issues as you present questions, but i'm not just going to give you the answer. Work for it! If you have a due date, set up a time you can be on here and i'll make sure i'll be on at that time and help you through it till we're done.

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

    Default Re: Program not printing anything

    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)
    {
    // changes the input text to lower case letters
    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)
    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();
    return largestLength();
    }
    }



    I have this thus far and when I run it the java input bars come up and then when I type things into it nothing is returned

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program not printing anything II

    Please start your own thread for your problem.

    Where is the class with the main() method. That is where the program's execution starts.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Program not printing anything II

    That's where my post went...i'll go grab my other post...

    --- Update ---

    Your printResults function does not print anything. You need to print what your longestLength() function is returning. And dont call it twice for that matter. Call it once, save it to a variable if necessary, then display it.

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

    Default Re: Program not printing anything II

    I don't understand how to change this at all. I understand that I shouldn't have printResults function because it is simply not doing anything but I don't get how to change it. Sorry so new to programming

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program not printing anything II

    I suggested this on the other thread. Learn the techniques by writing a small simple program.
    Then add to it to learn how to use methods.
    I suggest that you put this code aside for a while and work on a very simple program to see how to call a method.

    Write a new class with a main() method and one other method.
    Have the main() method create an object and call the other method.
    Have the other method print out a message.
    A Very simple program. When you get that working, then we'll work on adding some features one small step at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not printing anything II

    I'm really confused too. Here is a simple program that has a "max" method and tells you the larger integer that you input. Also, why does this program work in 1 file, but the letter frequency needs 2 files?

    import java.util.Scanner;  
     
    public class MaxNumber {
     
       public static void main(String[] args) 
       {
         Scanner scan = new Scanner(System.in);
          int i = scan.nextInt();
          int j = scan.nextInt();
          int k = max(i, j);
          System.out.println("The maximum between " + i +
                        " and " + j + " is " + k);
       }
     
     
       public static int max(int num1, int num2) {
          int big;
          if (num1 > num2)
             big = num1;
          else
             big = num2;
     
          return big; 
     
       }
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program not printing anything II

    Someone somewhere thinks that the main() method testing MUST be in a special class, not in the class that is being tested.
    Personally I think the testing code (the main() method) should be in the class that is being tested.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Program not printing anything II

    I have this for my driver class... But I don't know what to put so that it will print out the number count...I have made is so that there is a 2 line input

    import java.util.Scanner;

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

    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program not printing anything II

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    What is the purpose of the Scanner object and the while loop?


    Are you ignoring what I suggested in post#7?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Program not printing anything II

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



    That is what my professor said it is a scanner object... and the while loops scans the lines of text?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program not printing anything II

    while loops scans the lines of text?
    Why does the program want to scan lines of text? What is the purpose of doing that?

    The posted code is not formatted properly. Nested statements within {}s should be indented 3-4 spaces. All statements should NOT start in the first column.

    You should start by doing the sample programs in the tutorial. This assignment is past your current knowledge.
    See: "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Program not printing anything II

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

    sorry when I coped it lines got shifted. The reason for the scan lines is that it will scan the text that is inputed then will return letters and count of how many times they appear

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program not printing anything II

    For testing I suggest that you comment out the scanner and the loop and define a String to scan in the program.
    When the code can scan and report on that String, then you can restore the loop etc. For example:
    String toScan = "The string to scan would go here";
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program not printing anything
    By kbrady481 in forum What's Wrong With My Code?
    Replies: 38
    Last Post: March 19th, 2013, 09:08 PM
  2. Replies: 3
    Last Post: December 11th, 2012, 01:28 PM
  3. [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
  4. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 10:00 AM
  5. Triangle Printing - Java Program
    By mparthiban in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 18th, 2010, 05:35 AM