Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:24:46+00:00 2026-05-23T10:24:46+00:00

In the code below I am trying to calculate the distance between two cities.

  • 0

In the code below I am trying to calculate the distance between two cities. The user will enter a city name and a city, then the user will enter the distance between those cities, and finally enter the price to travel between those cities. I am not getting it to evaluate yet as I am unsure exactly how I am going to do this. My question is I am looking for pointers and suggestions on how one would do this.

import java.io.*;
import java.util.*;

public class CityCalcultor {

    static LinkedList<String> cities = new LinkedList<String>();
    static LinkedList<Integer> distance = new LinkedList<Integer>();
    static LinkedList<Integer> price = new LinkedList<Integer>();

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        String text;
        int option = 0;
        while (true) {
            System.out.println("\nWhat would you like to do:\n"
                + "1. Add a city to the system\n"
                + "2. Add a path to the system\n"
                + "3. Evalute paths\n"
                + "4. Exit\n" + "Your option: ");
            text = input.nextLine();
            option = Integer.parseInt(text);

            switch (option) {
                case 1:
                    EnterCity();
                    break;
                case 2:
                    EnterPath();
                    break;
                case 3:
                    EvalutePaths();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("ERROR INVALID INPUT");
            }
        }
    }

    public static void EnterCity() {
        String c = "";
        LinkedList<String> cities = new LinkedList<String>(Arrays.asList(c));
        Scanner City = new Scanner(System.in);
        System.out.println("Please enter the city name ");
        c = City.nextLine();
        cities.add(c);
        System.out.println("City " + c + " has been added ");
    }

    public static void EnterPath() {
        Scanner Path = new Scanner(System.in);
        int d = 0;
        int p = 0;
        System.out.println("Enter the starting city ");
        System.out.println();
        System.out.println(Path.nextLine());
        System.out.println("Enter the ending city ");
        System.out.println(Path.nextLine());
        System.out.println("Enter the distance between the two cities ");
        d = Path.nextInt();
        for (d = 0; d > 0; d++) {
            distance.add(d);
        }
        System.out.println("Enter the price between the two cities ");
        p = Path.nextInt();
        for (p = 0; p > 0; p++) {
            price.add(p);
        }
        System.out.println("The route was sucessfully added ");
    }

    private static void EvalutePaths() {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:24:47+00:00Added an answer on May 23, 2026 at 10:24 am

    There are two ways I can interpret your question: you either want to evaluate every possible path between pairs of cities (which is technically infinite, unless you restrict revisiting of cities), or you want to find the shortest distance/cheapest way to get from city to city.

    I don’t feel like trying to figure out the first one, and I’m pretty sure you mean the second, so I’m going with that.

    The solution to the problem is to model your cities and the routes between them using a graph, where each vertex is a city, and each edge is a direct path between two cities. The edges are weighted either by the distance between the cities, or by the cost of travel.

    You can then apply Dijkstra’s Algorithm (or one of its variants) to find the path between any source vertex (city of departure) to all other vertices (destination cities) with the smallest total weight (i.e. smallest distance or lowest cost). If you apply this algorithm using each vertex in turn as the source, you will have built a complete model of the cheapest routes between any two cities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the code below I'm trying to load some images and put them in
I'm trying to use the code below to send messages via System.Net.Mail and am
So far i have got the code below which works lovely when trying an
In my code attached below, I'm trying to upload a file via ASP.NET. I
Can anyone tell how correct the following code below. Iam tryin to create a
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I
The code below works. But if I comment out the line Dim objRequest As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.