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

Thread: Help with java array prgm

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    lansdale pa
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with java array prgm

    My problem is this: obviously i can follow directions and learn. However, I am having trouble developing the thinking development part or thinking in Java. Granted, this is my first computer science class but I'm getting discouraged and falling behind in my class. I want to learn how to write java not just take your perfectly written programs and hand them in as my own although that would get me an A, lol.

    So far i have
    // i'm attempting to say i want my array to be called tennumbs and have a column size of 10.
    int[] tennumbs = new int [10];
    //I have no idea what the next following line really means but i found in my book that it needed to be added. Im trying to declare my variable.
    for (int i = 0; i < data.length; I++);

    is this right so far and how do i prompt for input values? by using java.util.scanner?




    heres the requirements for the program.

    Working with single dimension arrays.

    Write a program that reads positive numbers (integers) and stores them into an array.

    1.Write a method to prompt for the input values. (sentinel terminated)

    2.Write a method to print the array.

    3.Write a method that returns the value of the smallest number.

    4.Write a method that returns the value of the largest number.

    5.Write a method to find the location of a number (array index + 1) .

    6.Write a method to compute the average of the numbers in the array.

    7.Write a method to output the results.

    The input prompt example

    Enter at least unique 10 positive numbers (integers) terminated with a sentinel value (-99)

    The output could look like this:

    Amount of numbers read: 10

    Numbers read: 12, 33, 41, 1, 4, 10, 7, 21, 66, 50

    Smallest number was 1 in location 4

    Largest number was 66 in location 9

    Average was 24.5


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

    Default Re: Help with java array prgm

    how do i prompt for input values
    Use the System.out.println() method to print a message to the user.
    The Scanner class has several method for reading the user's response.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    lansdale pa
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with java array prgm

    I'm trying to prompt the user for 10 seperate integers than save them as seperate elements in the array. So far i have this:
    import java.util.ArrayList;
    import java.util.Scanner;

    public class Arrays {
    public static void main(String args[]) {
    double [] data;
    data = new double [10];


    Scanner input = new Scanner(System.in);


    //allow user input
    System.out.println("Enter one integer from 0-20");
    int numb = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb1 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb2 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb3 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb4 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb5 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb6 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb7 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb8 = input.nextInt();
    System.out.println("Enter another integer from 0-20");
    int numb9 = input.nextInt();

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

    Default Re: Help with java array prgm

    Java SE has a class named Arrays. To avoid confusion, you should not use the same name as a java SE class.

    You only need to define ONE instance of the Scanner class. There is only one keyboard/user and one Scanner object can be used to get data from that one user.


    prompt the user for 10 seperate integers
    Use a loop:
    prompt the user
    read his response
    put the response in the array
    increment the array index to the next slot
    end of loop
    Some of the above steps are handled by a for loop

    When posting code, please 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
    Junior Member
    Join Date
    Nov 2012
    Location
    lansdale pa
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with java array prgm

    Hey Norm, did you understand and excell in Java right away? I feel like this shouldn't be this hard.

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

    Default Re: Help with java array prgm

    I've been programming for a long time. I started Java in 1998.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Location
    lansdale pa
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with java array prgm

    ok. im just gonna keep trying to work hard and hope i hit a revelation sometime lol.

    --- Update ---

    So i figured it out after a long time staring at my screen. All i need to do now is add a sentinel value to allow the user to enter less than 10 inputs.

    import java.util.Scanner;
     
    public class ArrayListProject {
    public static void main(String args[]) {
     
     
    	Scanner in = new Scanner(System.in);
     
    int[] squares=new int[10]; 
    	for(int i=0; i<10; i++)
    	{
    		System.out.println("Enter one integer from 0-20");
    		squares[i] = in.nextInt();
    	}
    	in.close();
    	printarray(squares);
    }
     
    public static void printarray(int[] a)
    {
    	for(int i=0; i<10; i++)
    		System.out.println(a[i]);
    }

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

    Default Re: Help with java array prgm

    If you use a sentinel and can have less than 10 entries, you'll want to have a variable that keeps track of the number of entries in the array. You could use it in the printarray method to only print the entered data and not the empty slots.

    The printarray method should use the length of the array, not a hardcoded 10 to end the for loop.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java 2d array problem
    By jugi in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 9th, 2012, 05:59 PM
  2. java array
    By fazmy3fvz in forum Java Theory & Questions
    Replies: 10
    Last Post: June 9th, 2012, 01:25 PM
  3. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM
  4. Java Uneven 2D Array
    By ba.accounts in forum Collections and Generics
    Replies: 5
    Last Post: August 10th, 2011, 07:38 AM
  5. Java Array
    By lary in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 09:29 AM