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

Thread: Arrays

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays

    How can I create an array directly from user input. Like read number to array until user input negative number (positive numbers should be the size of array).The input of negative number will be the trigger to stop the inputs.

  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: Arrays

    What have you tried to do to solve this problem?
    Do you have any specific java programming questions?

    Please wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    So the question is:
    I need to create a program that reads a set of positive integers (for an array). Number reading ends when a negative number is entered.
    Then determine which is the smallest element in the set and how many times it was inserted.
    The program must show the smallest number entered and how many times it was inserted, on separate lines and with the following format:
    smaller = <number>
    occurrences = <number of occurrences>

    import java.util.Scanner;
     
    public class program {
     
        public static void main(String[] args) {
            int length = 6; // the length should be automatic ends when input is negative
            int num = 0,  i = 0;
     
            int[] numero = new int[i];
            Scanner sc = new Scanner(System.in);
            while ((num >= 0) && (i < numero.length)) {
                num = sc.nextInt();
                if (num >= 0) {
                    numero[i] = num;
                    i++;
                }
            }
     
            smallerValue(numero);
     
        }
     
        public static int smallerValue(int[] arrvalue) {
            int smaller = arrvalue[0];
     
            for (int i = 1; i < arrvalue.length; i++) {
     
                if (arrvalue[i] < smaller) {
     
                    smaller = arrvalue[i];
     
                }
            }
            System.out.println("Smaller value from array" +smaller);
            return smaller;
        }
        public static int occurrencesValue(int[] arrvalue){
              /* not finhised*/
     
        }return occurence;
    }
    Last edited by EmaV; December 13th, 2020 at 09:56 AM.

  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: Arrays

    I need to create a program that
    Ok, start with the first item in the list that you are having problems with.
    Describe the item you are working on and what problems you are having trying to code it.
    Post any questions about the problems you are having writing the code for that item from the list.

    When you have solved that problem, move to the next item in the list.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    I'd probably try something alone the lines of this. Go through the array from start to finish and simply check if the current number is smaller than the next? If yes raise the number of occurances by 1; if not move to the next element.

Similar Threads

  1. need help with arrays
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2013, 08:46 PM
  2. Arrays
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 6
    Last Post: December 9th, 2011, 07:24 PM
  3. Can Anyone Help With Arrays?
    By metaleddie13 in forum Collections and Generics
    Replies: 9
    Last Post: November 16th, 2011, 12:12 AM
  4. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  5. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM

Tags for this Thread