2017-06-15 2 views
1

私は任務に取り組んでおり、過去にはできない壁にぶつかります。Stringからintに変換する方法がわからないのですか?

私のコードを続行するには、このユーザーを選択する方法を理解できません。

たぶん、あなたの最善の策は、keyboard.nextLine(の代わりにスキャナーを使用することです笑

import java.util.Scanner; 
import java.util.Arrays; 

    public class DestinationPlanner { 

static Scanner keyboard = new Scanner(System.in); 
static int cityDistance, arrivalCity; 
static double gasCost, MPG, totalCost; 
static String userContinue, userSelection, departCity; 

static double [][] cityTable = { 
    {0, 384, 706, 729, 744, 767, 605, 548, 492, 346}, 
    {384, 0, 399, 628, 437, 473, 291, 714, 390, 244}, 
    {706, 399, 0, 877, 47, 605, 110, 1086, 742, 640}, 
    {729, 628, 877, 0, 914, 384, 825, 395, 259, 406}, 
    {744, 437, 47, 914, 0, 609, 154, 1123, 780, 685}, 
    {767, 473, 605, 384, 609, 0, 563, 708, 367, 420}, 
    {605, 291, 110, 825, 154, 563, 0, 1002, 679, 533}, 
    {548, 714, 1086, 395, 1123, 708, 1002, 0, 344, 469}, 
    {492, 390, 742, 259, 780, 367, 679, 344, 0, 146}, 
    {346, 244, 640, 406, 685, 420, 533, 469, 146, 0}, 
}; 

static String [] citySelection = {"Jacksonville, Fl", "Charlotte, NC", "Washington, DC", "Memphis, TN", 
    "Baltimore, MD", "Louisville, KY", "Richmond, VA", "New Orleans, LA", "Birmingham, AL", "Atlanta, GA" 
}; 

public static void main(String[] args) { 
    System.out.println("Welcome to the Southeast Destination Planner!" 
      + "\nTo help you plan your trip this program will calculate" 
      + "\nthe cost to travel between two popular Southeast cities" 
      + "\nWill you continue?" 
      + "\nEnter 'y' to continiue 'n' to quit"); 
    userContinue = keyboard.nextLine().toLowerCase();  

    while ((userContinue.equals("y")) & (userContinue.equals("n"))){ 
     userContinue = keyboard.nextLine().toLowerCase(); 
    } 
    while (userContinue.equals("n")) { 
     System.out.println("The program will now terminate"); 
     System.exit(0); 
    } 

    while (userContinue.equals("y")) { 

     citySelection(); 
     carInfo(); 
     finalCost(); 

    } 

} 

private static void citySelection() { 
    System.out.println("\nGreat, let's start by determining the city you will be departing"); 
    System.out.println("\nPlease choose from the following cities"); 
    System.out.println("[0] Jacksonville, FL"); 
    System.out.println("[1] Charlotte, NC"); 
    System.out.println("[2] Washington, DC"); 
    System.out.println("[3] Memphis, TNA"); 
    System.out.println("[4] Baltimore, MD"); 
    System.out.println("[5] Louisville, KY"); 
    System.out.println("[6] Richmond, VA"); 
    System.out.println("[7] New Orleans, LA"); 
    System.out.println("[8] Birmingham, AL"); 
    System.out.println("[9] Atlanta, GA"); 

    System.out.println("\nWhere will your trip begin?"); 
    departCity = keyboard.nextLine(); 

    while (!(departCity.equals("1")) && !(departCity.equals("2")) && !(departCity.equals("3")) && 
      !(departCity.equals("4")) && !(departCity.equals("5")) && !(departCity.equals("6")) && 
       !(departCity.equals("7")) && !(departCity.equals("8")) && !(departCity.equals("9")) && 
         !(departCity.equals("0"))){ 

     System.out.println("Please enter a number 0 through 9"); 

    departCity = keyboard.nextLine(); 
    } 
    System.out.println("\nYou have chosen to depart from " + citySelection[departCity]); 



} 

答えて

2

(念の入力は数値ではありませんでした) 。その後、オートボクシングはそれを処理する必要がありますが、ヌルまたは0を取得するケースを探したいでしょう。さらに、9つの異なる条件のif文ではなく、範囲内にあることを確認することもできます。

+0

または 'Integer.parseInt'によって返される' int'の代わりに 'Integer'が必要な場合は' Integer.valueOf' – alfasin

+1

ありがとう、ありがとう!これはそれを修正しました!ほんとうにありがとう。 –

3

私は思考上だけど、私は途方に暮れだとあまりにも長い間、この時に開始しています) 。それはトライキャッチで囲まれる必要があるものの、それは、右のそれを建て.nextInt()を持っているあなたは、Integerオブジェクトに解析するInteger.parseInt()を使用することができます

Scanner s = new Scanner(System.in); 
    int result = s.nextInt(); 
関連する問題