Expanding my program

104 views Asked by At

So i got a programm that asks the user to type in the 3 favorite cities , it stores them in Array and prints them later out to the user. What im trying to do now is making the programm ask the user for how many favorite cities he/she got?the user types in a number wich will give the user the oppertunity to type in that ammount of favorit cities and later on print them out.

Problem is i really have no idea how to do this could anyone help?

Please explain the code incase you help so i can understand :) , Sorry for my bad English not my primary language!

My code atm looks like this :

package com.example.array.main;

import java.util.Scanner;

public class Application {

    public static void main(String[] args) {


        String[] favoritCity = new String [3];
        Scanner scanner1 = new Scanner (System.in);

        System.out.println("skriv in 3 favoritstäder");
        String userIn1 = scanner1.nextLine();
        String userIn2 = scanner1.nextLine();
        String userIn3 = scanner1.nextLine();


        favoritCity[0] = userIn1;
        favoritCity[1] = userIn2;
        favoritCity[2] = userIn3;


        System.out.println(userIn1);
        System.out.println(userIn2);
        System.out.println(userIn3);

            }

    }
3

There are 3 answers

1
Barbe Rouge On

Before to define your Array size, you can use your scanner to ask for the number of cities, for example:

    Scanner scanner = new Scanner(System.in);
    System.out.println("How many cities?");
    int nberCities = scanner.nextInt();

    String[] favoriteCities = new String[nberCities];
    for(int i = 0; i < nberCities;i++){
      favoriteCities[i] = scanner.nextLine();
    }
0
Subhrajyoti Majumder On

First take a integer input from user, that how many favorite cities does he/she have? Scanner#nextInt(); which could help you get integer input from console. Create a array with that size Then write loop like -

String[] favoritCity = new String [noOfFabCities];
for(int i=0;i<maxFavCities;i++){
    ...
}

Now inside the loop get the input from user Enter your i+1 (as i starts with 0) favorite city and capture the input like Scanner#nextLine();. and keep the value into your array like -

favoritCity[i] = userInput;

Once you get all the user input, now can you print in console all the user's favorite cities.

0
kai On

That should be the code which are you looking for. I don't tested it, but it should works.

package com.example.array.main;

import java.util.Scanner;

 public class Application {

    public static void main(String[] args) {

        int cityNumb;

        Scanner scanner1 = new Scanner (System.in);

        System.out.println("How many favorite cities do you have?");
        cityNumb = scanner1.nextInt();

        //creat the array with the size of cityNumb
        String[] favoritCity = new String [cityNumb];

        System.out.println("skriv in "+cityNumb+" favoritstäder");

        //for loop for the input of the cities
        for(int i = 0; i < cityNumb; i++) 
        { 
            String city = scanner1.nextLine();
            favoritCity[i] = city;
        }

        //for loop for the output of the cities
        for(int i = 0; i < cityNumb; i++) 
        { 
            System.out.println(favoritCity[i]);
        }

    }

}