Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Is there a way top populate a 2d array using a nested while loop?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im working on an assignment that requires me to populate a 2d array using a nested while loop? Any ideas on how this is done? The 2d array is 3x3
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static double[][] score() {
       int col = 3;
       int row = 3;
       int size = 0;
       Scanner in = new Scanner(System.in);
       double[][] scores = new double[row][col];
       System.out.println("Enter your scores: ");
       while (in.hasNextDouble() && size < scores.length) {
           while (size < scores[size].length) {
               scores[][] = in.hasNextDouble();
               size++;
           }
           return scores;
       }


I learned it from stackoverflow website.
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Murphy Brown wrote: . . . I learned it from stackoverflow website.

Did you copy it exactly? If it is from here, you did copy it correctly. And it won't work because you only have one loop variable. I shall let you work out what will happen. Don't use hasNextDouble() in the loop condition.
As the replies on SO said, it is usual to use nested for loops to populate arrays like that; I would prefer not to use loops at all. For an ordinary array, I would use a DoubleStream, which you can create from a Random object with methods like this one.If you go through the Random API, you will find several other methods that create XXXStreams. It is a bit more awkward for arrays of arrays. If you are going to use nested for loops, you would end up with something like this:-Note the array initialiser creates a “jagged” array one of whose elements is length zero. That is a legitimate thing to do.
Since all loops (except using continue in a for loop) can be implemented with while, let's try that:-That has two disadvantages compared to a for loop:-
  • 1: You need an extra line to increment the loop variables, and you have to be very careful where you put the incrementation.
  • 2: The loop variables remain in scope after the loop finishes. You can solve that problem with additional {}s.
  •  
    Irvin Florencia
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I made the adjustments and then added a second method that would calculate the average of each row. However, when I ran it gave me wrong results.



    The program is supposed to take user input to fill the array and use three methods. the first method populates the 2d array while using nested while loop. The second calculates the average of the numbers in each row. And the last displays the numbers in the array and the average of each row. I don't what I'm doing wrong. I'm new to Java.
     
    Campbell Ritchie
    Marshal
    Posts: 63842
    209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please have a look at this link about how the code button works. I have corrected the code tags for you just this once and you can see how much better it looks
    But make sure to put spaces before each {, don't put so many emty lines inside a method, and put one empty lien between successive methods.

    Don't use a for loop to iterate your arrays; use a for‑each loop, as in this tutorial (where it is called by its official name, enhanced for). Go through the loop very carefully (lne 31) with a pencil and work out the value of sum as you iterate the individual arrays. And work out how often you are printing values. Also have a close look at the behaviour of the += and similar operators, because I don't think it does what you think it does.
     
    You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
    global solutions you can do at home or in your backyard
    https://www.kickstarter.com/projects/paulwheaton/better-world-boo
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!