Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Printing array elements

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write three statements to print the first three elements of array runTimes. Follow each statement with a newline. Ex: If runTime = {800, 775, 790, 805, 808}, print:
800
775
790

import java.util.Scanner;

public class PrintRunTimes {
  public static void main (String [] args) {
     Scanner scnr = new Scanner(System.in);
     final int NUM_ELEMENTS = 5;
     int [] runTimes = new int[NUM_ELEMENTS];
     int i;

     for (i = 0; i < runTimes.length; ++i) {
        runTimes[i] = scnr.nextInt();
     }

     /* Your solution goes here  */
     // Populate array
     runTimes[0] = 800;
     runTimes[1] = 775;
     runTimes[2] = 790;
     runTimes[3] = 805;
     runTimes[4] = 808;
     
     for ( i = 0 ; i < 3; i++){
        System.out.println(runTimes[i]);}

  }
}

Testing with inputs: 800 775 790 805 808
Your output
800
775
790

Testing with inputs: 10 25 -10 20 -15
Output differs. See highlights below.
Your output
800
775
790

Expected output
10
25
-10
 
Master Rancher
Posts: 4839
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do the 4 lines at the end mean?  Is that the problem?  Where do those 3 numbers come from?

Please be sure to wrap all posted code in code tags: Select the code and press the Code button.
 
Saloon Keeper
Posts: 9442
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've hard coded the values of the array, how would you ever get an output other than what you are getting?
 
Marshal
Posts: 76121
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what your code would have looked like with code tags. I shall lef you wokr out what the formatting error in line 23 is.Apart from the fact that you are populating your array twice and the first time is pointless, I think an array initialiser is a far easier and less error‑prone to populate a new array:-
 
Norm Radder
Master Rancher
Posts: 4839
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Testing with inputs: 10 25 -10 20 -15


Looks like the program is being run in a tester that provides the input to be put in the array.  The code should Not be putting its own values in the array.
 
Carey Brown
Saloon Keeper
Posts: 9442
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 11 is getting the values from the tester, but then you overwrite those values on lines 16-20.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic