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 10 of 10

Thread: Not printing in console. What am i missing?

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Not printing in console. What am i missing?

    Morning. Working on a project. It builds and compiles but it doesnt display data in the console ? I have an input file that i am using but cant understand why I dont see anything in the console.
    Below is the code. I have no errors when compiling.

    What am i missing?



    This is the input text for the input.txt file

    This is a test, this is only a test!
    This test is a test of your Java programming skills!
    xyz
    ABCDEFGHIJKLMONPQRSTUVWXYZ1234567890-=_+ !
    end data
    public class Project1 
    {
        /**
         * @param args the command line arguments
         * @throws java.io.FileNotFoundException
         */
        public static void main(String[] args) 
                                throws FileNotFoundException, IOException
        {   
          FileReader inputStream = new FileReader("input.txt");       
     
            class StringConversion
            {
            public String lowerCaseString = "";
     
                public String lowerCaseString(String trimmed)
                    {
                    lowerCaseString = trimmed.toLowerCase();
                    return this.lowerCaseString;
                    }
            }
        }
     
           public static class ThreeMethods
        {
         //white space
     
                public static String trimmed(String s)
                {
                    if (s == null)
                    {
                        return "";
                    }
                    return s.trim();
                }
            //return a trimmed string of size len
            public static String trimmed(String s, int len)
            {
                String retVal = ThreeMethods.trimmed(s);
                if (len > retVal.length())
                {
                    len = retVal.length();
                }
                return retVal.substring(0,len);
            }
            //convert all double spaces to a single space
            public static String squeeze(String s)
            {
                return s.replace("  ", " ");
            }
        }
     
            //This method will read strings from the input file 
        //and perform manipulations on each string. The results of the 
        //manipulations are displayed in the text area
     
           private void displayStrings() 
                                throws Exception
        {         
            FileReader inputStream = new FileReader("input.txt");
            BufferedReader reader= new BufferedReader(inputStream);
     
            //        
            //Get the next line in the file                        
            //
                String str = reader.readLine();
     
     
                            //Trim and Squeeze
                    System.out.print("Trim & Squeeze: " + ThreeMethods.squeeze(ThreeMethods.trimmed(str)) + "\n");
                    //Trim, Squeeze, and Shorten to 10 characters
                    System.out.print("Trim, Squeeze, Shorten to 10: " + ThreeMethods.trimmed(ThreeMethods.squeeze(ThreeMethods.trimmed(str)),10) + "\n");
                    //Trim, Squeeze, lower-case and shorten to 20 characters
                    System.out.print("Trim, Squeeze, Shorten to 20, lower-case: " + ThreeMethods.trimmed(ThreeMethods.trimmed(ThreeMethods.squeeze(ThreeMethods.trimmed(str)),20)) + "\n");
                    System.out.print("\n");
     
     
                }
        }
    }
    Last edited by crgb1202; April 2nd, 2018 at 08:11 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Not printing in console. What am i missing?

    What code should print on the console?
    How is that code being executed?
    If the print statements are not executed, nothing is printed.
    Starting from the main method, how do the print statements get executed?

    Are there any compiler errors? Copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not printing in console. What am i missing?

    Thank you for responding.
    The input text that was edited in the strings (making lower case, removing white space) is what should display in the console.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Not printing in console. What am i missing?

    Ok that answers my first question. Now what about the next questions I asked:
    2)How is that code being executed?
    If the print statements are not executed, nothing is printed.
    3)Starting from the main method, how do the print statements get executed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2018
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not printing in console. What am i missing?

    Should i have the file reader in the beginning before the three methods? The three methods should print out?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Not printing in console. What am i missing?

    The three methods should print out?
    A method is not executed unless it is called.
    Where does the code call any of the methods?

    Do you know what I mean when I say "call a method"?
    lowerCaseString = trimmed.toLowerCase(); // this statement calls the toLowerCase method
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2018
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not printing in console. What am i missing?

    I have them in the argument of the print statement - or am i wrong?
    System.out.print("Trim & Squeeze: " + ThreeMethods.squeeze(ThreeMethods.trimmed(str)) + "\n");

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Not printing in console. What am i missing?

    I think you are missing how the java program executes your code. It starts by calling the main() method. Then each statement in the main method is executed in turn. When the end of the main method is reached, execution for your program stops. (This is modified when other threads are involved)
    If the main method does not call any other methods, those other methods are not executed.

    For a method to be executed, it must be called. Where does the main method call any methods?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    crgb1202 (April 2nd, 2018)

  10. #9
    Junior Member
    Join Date
    Apr 2018
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not printing in console. What am i missing?

    Quote Originally Posted by Norm View Post
    I think you are missing how the java program executes your code. It starts by calling the main() method. Then each statement in the main method is executed in turn. When the end of the main method is reached, execution for your program stops. (This is modified when other threads are involved)
    If the main method does not call any other methods, those other methods are not executed.

    For a method to be executed, it must be called. Where does the main method call any methods?
    Looking through my book to see if i can figure out how to call the methods.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Not printing in console. What am i missing?

    Take a look at the tutorial: https://docs.oracle.com/javase/tutor...arguments.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  2. printing in java; missing spaces
    By user85116 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 02:51 PM
  3. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  4. Printing A TreeNode/Node To Console..
    By Kumarrrr in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 17th, 2010, 12:35 AM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM