簡體   English   中英

我無法解決Dijkstra算法程序中的String索引異常問題

[英]I can't solve the String index exception problem in Dijkstra algorithm program

因此,這是線程“主”中的舊異常java.lang.StringIndexOutOfBoundsException問題。

更准確地說,(字符串索引超出范圍:-1)

顯然,它是復制和粘貼代碼,否則我不會遇到任何問題。

這是完整的代碼,相關代碼段如下:

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

public class Proj4 

public static String[] cities;
public static int[][] mileage;
public static boolean[] visited;
public static int[] prev;
public static int[] dist;
public static int size;

public static void main(String[] args) throws IOException
{
    Scanner fin = new Scanner(new File("cities.txt.txt"));
    Scanner in = new Scanner(System.in);
    size = Integer.parseInt(fin.nextLine());
    cities = new String[size];
    mileage = new int[size][size];
    visited = new boolean[size];
    prev = new int[size];
    dist = new int[size];
    for(int i = 0; i < size; ++i)
    {
        cities[i] = fin.nextLine();
    }
    String line = null, city1, city2;
    int distance;
    for(int i = 0; i < size; ++i)
    {
       for(int j = 0; j < size; ++j){
           mileage[i][j] = Integer.MAX_VALUE;
    }
}

以下是相關代碼段:

  while(fin.hasNextLine())
  {
    line = fin.nextLine();
    city1 = line.substring(0, line.indexOf(','));
    city2 = line.substring(line.indexOf(',') + 1, line.indexOf(':'));
    distance = Integer.parseInt(line.substring(line.indexOf(':') + 1));
    mileage[index(city1)][index(city2)] = distance;
    mileage[index(city2)][index(city1)] = distance;
  }

    while(true)
    {
        System.out.print("Enter the first city: ");
        city1 = in.nextLine();
        System.out.print("Enter the second city: ");
        city2 = in.nextLine();
        int startInd = index(city1);
        int stopInd = index(city2);
        if(startInd == -1 || stopInd == -1){
            System.out.println("Invalid city name(s)");
     }
        else
        {
            dijkstra(city1);
            print(city1, city2);
        }
        System.out.print("Another pair? (Y/N): ");
        line = in.nextLine();
        if(line.equalsIgnoreCase("N"))
        {
            break;
        }
    }
}

public static int index(String name)
{
    for(int i = 0; i < cities.length; ++i)
    {
        if(cities[i].equalsIgnoreCase(name))
        {
            return i;
        }
    }
    return -1;
}

public static void init(String start) {
    int startInd = index(start);
    for(int i = 0; i < size; ++i){
        dist[i] = Integer.MAX_VALUE;
        prev[i] = -1;
        visited[i] = false;
    }
    dist[startInd] = 0;
}

public static int minIndex()
{
    int minInd = -1, minDistance = Integer.MAX_VALUE;
    for(int i = 0; i < size; ++i)
    {
        if(!visited[i] && dist[i] < minDistance)
        {
            minInd = i;
            minDistance = dist[i];
        }
    }
    return minInd;
}

public static boolean done()
{
    for(int i = 0; i < size; ++i)
    {
        if(!visited[i])
        {
            return false;
        }
    }
    return true;
}

public static void print(String start, String stop)
{
    int startInd = index(start);
    int stopInd = index(stop);
    List<String> intCities = new ArrayList<>();
    int prevInd = prev[stopInd];
    while(prevInd != startInd)
    {
        intCities.add(cities[prevInd]);
        prevInd = prev[prevInd];
    }
    System.out.print(start + "->");
    for(int i = intCities.size() - 1; i >= 0; --i)
    {
        System.out.print(intCities.get(i) + "->");
    }
    System.out.println(stop + ", distance " + dist[stopInd] + " miles");
}

public static void dijkstra(String start)
{
    init(start);
    while(!done())
    {
        int x = minIndex();
        visited[x] = true;
        for(int y = 0; y < size; ++y)
         {
            if(!visited[y])
            {
                if(mileage[x][y] != Integer.MAX_VALUE)
                {
                    int dy = dist[x] + mileage[x][y];
                    if(dy < dist[y])
                    {
                        dist[y] = dy;
                        prev[y] = x;
                    }
                } 
            }
         }
      }
   }
}

不好意思的格式,這是我的第一次問。 謝謝!

您的輸入字符串可能沒有','(逗號)或':'(冒號),因此indexOf()方法將在相關的代碼段中返回-1,這將導致java.lang.StringIndexOutOfBoundsException 字符串索引輸出的范圍:-1

通過在line = fin.nextLine();上啟用調試器來檢查輸入line = fin.nextLine();
或者在輸入System.out.println(line)之后添加print語句

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM