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

Thread: nothing prints after runing code

  1. #1
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default nothing prints after runing code

    hey guys. after running my code nothing comes out in the console for some reason (i have no errors)

    here is my code (it's a method example by my lecturer that i'm looking over)
    import java.util.Random;
    import java.util.Scanner;
     
    public class Test
    {
    	public static void main(String[] args)
    	{
    		int[] avals = createArray();
    		int[] bvals = createArray();
     
    		fillArrayRandomValues(avals);
    		printArray(avals, "AVals");
     
    		fillArrayRandomValues(bvals);
    		printArray(bvals, "BVals");
     
    		int atotal = calculateTotals(avals);
    		int btotal = calculateTotals(bvals);
     
    		printAnalysis(atotal, btotal);
    	}
     
    	private static void printAnalysis(int atotal, int btotal)
    	{
    		if(atotal > btotal)
    		{
    			System.out.println("A is greater than B");			
    		}
    		else if(btotal > atotal)
    		{
    			System.out.println("B is greater than A");
    		}
    		else
    		{
    			System.out.println("A is equal to B");
    		}
    	}
     
    	private static int calculateTotals(int[] vals)
    	{		
    		int total = 0;
    		for(int i = 0; i < vals.length; i++)
    		{
    			total = total + vals[i];
    		}		
    		return total;
    	}
     
    	private static void printArray(int[] vals, String title)
    	{
    		System.out.print(title + ":\t");
    		for(int i = 0; i < vals.length; i++)
    		{
    			System.out.print(vals[i] + "\t");
    		}		
    	}
     
    	private static void fillArrayRandomValues(int[] vals)
    	{
    		Random r = new Random();
    		for(int i = 0; i < vals.length; i++)
    		{
    			vals[i] = r.nextInt(1000);
    		}
    	}
     
    	public static int[] createArray()
    	{
    		Scanner input = new Scanner(System.in);
    		int size = input.nextInt();
    		int[] vals = new int[size];
    		return vals;			
    	}
    }

    anyone know why it is not printing anything out in the console?

    thanks in advance
    Last edited by darego; March 11th, 2011 at 10:09 AM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: nothing prints after runing code

    int size = input.nextInt();
    Here you are asking for input from the user, so when you run the program, input a number twice (you call createArray() twice) and it should display something for you.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    darego (March 11th, 2011)

  4. #3
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: nothing prints after runing code

    i don't know how i didn't realise that. thanks a lot mate! i'm used to having the console asking me for input first
    Last edited by darego; March 11th, 2011 at 10:16 AM.

Similar Threads

  1. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM