• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

arrays java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys i need help with the start of my assignment im very stuck

Allow the user to enter a number of race results (A minimum of two participants can run in a race and a maximum of 7). i think using array would be better ?
import java.util.Scanner;

public class ITBAthleticsClub
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int entrants=0; //declaring entrants


while(true)
{
System.out.println("Please enter the number of runners ranging from 2 to 7:");
//asks how many runers will take part in race
int entrants = input.nextInt();
input.nextLine();
// now we assign a possible value

if (entrants < 2) {
           System.out.println("*Error* please re-type you need at least 2 runners for a race.");
       }
       else if (entrants > 7) {
           System.out.println("You have entered too many runners for this race (max runners is 7).");
       } else

         
   {

System.out.printf("You have entered %d runners\n", entrants);
// define & initialize array
String[][] runners = new String[entrants][2];break; //loop exit point
}

}

     




 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Sophia tyran!

In the future, please UseCodeTags (that's a link) when you post code.  

Since we don't know what the variable runners will be used for, it's hard to know if an array of arrays is a good fit.  A more object-oriented way to do that is you have a List of Runner (Runner being a class you write with two fields).
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sophia tyran wrote:I have set up a code but it wouldnt run


Do you get an error?  If so, please post the full error message.  Otherwise, please tell us what "wouldn't run" means.
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic