6598 Shares

In Java, how do I add doubles to an array through a while loop?

In Java, how do I add doubles to an array through a while loop? Topic: How to write a loop in java
June 19, 2019 / By Mariel
Question: I'd appreciate being told how to add data to an array multiple times (If its possible) What I'm trying to do: Get the user to input a string of doubles, then use a while loop on a scanner to search that string for doubles, when one is found, add it to an array. The problem: I know there will only be 300 items entered, and I can create the array "double[] scores = new double[300];" however, I would like to add doubles to this array at multiple times (through a while loop). Could someone please write code which will search a string, extract the doubles and insert them into an array.
Best Answer

Best Answers: In Java, how do I add doubles to an array through a while loop?

Korrine Korrine | 5 days ago
If the doubles are separated by some character, such as a space, it is fairly easy to do. I would recommend a for loop instead of a while loop in this situation. String input; String[] temp = input.split(" "); int num_entered = temp.length(); double[] scores = new double[num_entered]; for(int i=0;i
👍 158 | 👎 5
Did you like the answer? In Java, how do I add doubles to an array through a while loop? Share with your friends

We found more questions related to the topic: How to write a loop in java


Korrine Originally Answered: In Java, how do I add doubles to an array through a while loop?
If the doubles are separated by some character, such as a space, it is fairly easy to do. I would recommend a for loop instead of a while loop in this situation. String input; String[] temp = input.split(" "); int num_entered = temp.length(); double[] scores = new double[num_entered]; for(int i=0;i
Korrine Originally Answered: In Java, how do I add doubles to an array through a while loop?
OK, some of the considerations: 1. Scanner, which I have come to appreciate, works best in this combination... a. -- System.out.print("Enter line: "); // cursor hangs at the end of the print message b. scanner.nextLine(); // to the user it looks like cursor was waiting // line is sucked in and echo to console // it a String, which is good 2. scanner.nextDouble() might have a use with a specific data entry program. I do not see how to wipe out the input buffer to start afresh after using anything else but scanner.nextLine(); 3. Validation, uses a method boolean. Pretty common scheme. Otherwise, we RegEx, count char, see if those char are in or out of a certain range. In our case, we ask java if a type double is possible at all with this String. 4. You could use a while for the input line, but why? Modern code is String s = blah, blah, blah String[] sArray = s.split(" "); StringTokenizer is discouraged. ============== import java.util.Scanner; public class GatherDoubles { static double temp; double[] scores; private void mess(String m) { System.out.print(m); } private void liner() { System.out.println("==================")... } private boolean validateDouble( String s ) { try{ Double num=Double.parseDouble(s); temp = num; return true; }catch(NumberFormatException e){} return false; } private void intro() { Scanner scanner = new Scanner(System.in); scores = new double[300]; System.out.println("Look for Doubles"); liner(); int arrCount = 0; boolean run = true; while (run) { mess("Input a line with a double: "); String[] inputWords = scanner.nextLine().split( " " ); int bellRing = 0; for (int i = 0; i < inputWords.length; i++) { if( validateDouble( inputWords[i] ) ) { mess("Thus far, in the inputed line, there has been " + ++bellRing + " doubles\n"); scores[arrCount] = temp; arrCount++; mess("and, baby, we've got "+arrCount+" in buckets\n"); } } run = ending( scanner, run); } } private boolean ending(Scanner scanner, boolean run) { mess("Would you like to do another? [ y / n ] :"); String answer = scanner.nextLine(); run = (answer.equalsIgnoreCase("y")) ? true : false; mess("\n"); mess("===============\n"); mess(run ? "User said YES. Do it again!!\n" : "User typed NO. Close program... BYE!\n"); return run; } public static void main(String[] args) { GatherDoubles gd = new GatherDoubles(); gd.intro(); } }

Jaydon Jaydon
OK, some of the considerations: 1. Scanner, which I have come to appreciate, works best in this combination... a. -- System.out.print("Enter line: "); // cursor hangs at the end of the print message b. scanner.nextLine(); // to the user it looks like cursor was waiting // line is sucked in and echo to console // it a String, which is good 2. scanner.nextDouble() might have a use with a specific data entry program. I do not see how to wipe out the input buffer to start afresh after using anything else but scanner.nextLine(); 3. Validation, uses a method boolean. Pretty common scheme. Otherwise, we RegEx, count char, see if those char are in or out of a certain range. In our case, we ask java if a type double is possible at all with this String. 4. You could use a while for the input line, but why? Modern code is String s = blah, blah, blah String[] sArray = s.split(" "); StringTokenizer is discouraged. ============== import java.util.Scanner; public class GatherDoubles { static double temp; double[] scores; private void mess(String m) { System.out.print(m); } private void liner() { System.out.println("==================")... } private boolean validateDouble( String s ) { try{ Double num=Double.parseDouble(s); temp = num; return true; }catch(NumberFormatException e){} return false; } private void intro() { Scanner scanner = new Scanner(System.in); scores = new double[300]; System.out.println("Look for Doubles"); liner(); int arrCount = 0; boolean run = true; while (run) { mess("Input a line with a double: "); String[] inputWords = scanner.nextLine().split( " " ); int bellRing = 0; for (int i = 0; i < inputWords.length; i++) { if( validateDouble( inputWords[i] ) ) { mess("Thus far, in the inputed line, there has been " + ++bellRing + " doubles\n"); scores[arrCount] = temp; arrCount++; mess("and, baby, we've got "+arrCount+" in buckets\n"); } } run = ending( scanner, run); } } private boolean ending(Scanner scanner, boolean run) { mess("Would you like to do another? [ y / n ] :"); String answer = scanner.nextLine(); run = (answer.equalsIgnoreCase("y")) ? true : false; mess("\n"); mess("===============\n"); mess(run ? "User said YES. Do it again!!\n" : "User typed NO. Close program... BYE!\n"); return run; } public static void main(String[] args) { GatherDoubles gd = new GatherDoubles(); gd.intro(); } }
👍 60 | 👎 3

Evelyn Evelyn
it could truthfully make it easier to to study the Java tutorials (Google it) or to get a progamming fundamentals (in Java) textual content textile. participant[] arr = new participant[3]; arr[0] = new participant("Bob"); //one way participant p = new participant("Chris"); arr[a million] = p; //yet another participant[] arr = new participant[]{ new participant("Chris"), new participant("Bob") }; //and yet another
👍 56 | 👎 1

Evelyn Originally Answered: Storing an array in another array in C?
I am not sure what operating system or compiler you are using but you may have to allocate memory for count-array (or you can alloc it elsewhere and pass count_array to the funtion also. count_array [] += array[i+1]-array[i]; would probably have to be: count_array [i] += array[i+1]-array[i]; else the loop will assign the result difference each time to the same location. My C is a little rusty hope this helps

If you have your own answer to the question how to write a loop in java, then you can write your own version, using the form below for an extended answer.