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.
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.
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.
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.
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.
Ok, start with the first item in the list that you are having problems with.I need to create a program that
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.
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.