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

Thread: A program with input and output on different method besides the main method

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A program with input and output on different method besides the main method

    The code posted below is an occurrence problem with how many times a number from 1-100 occurs, program stops executing when user enters 0. As for my homework assignment, I need to have the input and output in separate methods besides the main method. The first thing I did was write the whole program, then I got the input/output to another method... now I am stuck. I can't seem to figure out how to get the input and output to be in their own methods. Please help and thanks!


    package numberse;
     
    import java.util.Scanner;
     
    public class NumberSe {
     
     
        public static void main(String[] args) {
     
            System.out.println("FUN");
            userInput(args);
     
        }    
     
     
        public static void userInput(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] count = new int[100];
            int userIn = 0;
            for (userIn = 0; userIn < 100; userIn++) {
                count[userIn] = 0;
            }
            System.out.print("Enter the integers between 1 and 100: ");
            userIn = input.nextInt();
            while (userIn != 0) {
                count[userIn]++;
                userIn = input.nextInt();
            }
            for (userIn = 0; userIn < 100; userIn++) {
                if (count[userIn] != 0) {
                    if (count[userIn] > 1) {
                        System.out.println(userIn + " occurs " + count[userIn] + " times");
                    } else {
                        System.out.println(userIn + " occurs " + count[userIn] + " time");
                    }
                }
            }
        }
    }
    Last edited by tommyng; October 30th, 2011 at 07:49 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: A program with input and output on different method besides the main method

    One of the problems you will face is how to access data in the different methods. Say you have one method to input all the numbers, how do you access those numbers in another method to display the results. You have two options. Declare a bunch of instance variables that all methods have access to (this will require creating an object in the main method to start things off). Or you can pass all the data about as parameters.

    There's soemthing to get you started thinking about.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A program with input and output on different method besides the main method

    I just learned how to do arrays.. I'm not sure how to do it, but I tried changing the code and changed it to this. But now I am stuck, please help!

    package numberse;
     
    import java.util.Scanner;
     
     
    public class NumberSe {
     
     
       public static void main(String[] args) {
                   System.out.println("FUN");
            userInput(args);
     
        }    
     
     
        public static void userInput(String[] args) {
            Scanner input = new Scanner(System.in);
     
            int userIn = 0;
            for (userIn = 0; userIn < 100; userIn++) {
            }
            System.out.print("Enter the integers between 1 and 100: ");
            userIn = input.nextInt();
            while (userIn != 0) {
            printOut(userIn);
     
                userIn = input.nextInt();
            }
       }
     
     
     
       public static void printOut(int score) {
     
            int[] count = new int[100];
                    for (score = 0; score < 100; score++) {
                count[score] = 0;
            }
     
            while (score != 0) {
                      count[score]++;
            }     
     
            for (score = 0; score < 100; score++) {
                if (count[score] != 0) {
                    if (count[score] > 1) {
                        System.out.println(score + " occurs " + count[score] + " times");
                    } else {
                        System.out.println(score + " occurs " + count[score] + " time");
                    }
                }
            }
     
     
       }
    }

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: A program with input and output on different method besides the main method

    Ok, a few things.

    Arguments:
    In your
    public static void userInput(String[] args)
    {
      //Code Eluded
    }
    method, you don't actually ever use the parameter args. It would be better if it is just
    public static void userInput()
    {
      //Code Eluded
    }

    Indenting/Syntax:
    I recommend you look at Java Style Guide: Formatting: White Space

    Array Help:
    http://download.oracle.com/javase/tu...ts/arrays.html
    Initializing Arrays:
    Specifically:
    int[] count = new int[100];
    for (score = 0; score < 100; score++) {
      count[score] = 0;
    }

    That code doesn't do anything different than
    int[] count = new int[100];
    because it automatically initializes all the variables in the array to 0.

    And finally:
    How To Ask Questions The Smart Way
    Be Precise
    Last edited by Tjstretch; October 31st, 2011 at 12:45 AM. Reason: bbcode fail

Similar Threads

  1. How to call a value from a different method to main
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 22nd, 2011, 06:29 PM
  2. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  3. [SOLVED] method inside main()
    By rohan22 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2011, 11:42 PM
  4. Java main method args
    By mattxo in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 09:45 AM
  5. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM