2016-09-25 6 views
3

아래 코드에 대한 도움이 필요합니다. 나는 사용자가 'N'을 선택할 때까지 프로그램을 반복하려고하는데, 그때 레모네이드에 대한 합계를 온스와 총 쿠키 및 각 종류의 셔츠로 판매하고 싶습니다.문자열 인덱스가 범위를 벗어 났으며 입력 데이터를 사용하는 합계

나는 루프 프로그램에 while 루프에 넣어하지만 오류 얻을 : 스레드에서

예외를 "주"java.lang.StringIndexOutOfBoundsException : 문자열 색인이 범위를 벗어 : 0 java.lang.String의에서 .charAt (알 수없는 출처) at edu.kirkland5.SnackStand.main (SnackStand.java:43)

누구나 내가이 두 가지를 성취하기 위해해야 ​​할 일을 도울 수 있다면 크게 감사하겠습니다. 고맙습니다!

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 

    String userName = ""; 
    int lemonaid = 0; 
    double OZ12_PRICE = 1.50; 
    double OZ16_PRICE = 2.00; 
    char cookies = 0; 
    double COOKIES_OAT= .75; 
    double COOKIES_CHOCO= .75; 
    char tshirt = 0; 
    double TSHIRT_Y = 15.00; 
    double TSHIRT_N = 8.00; 
    char item; 
    char answer = 0; 

    System.out.println("Hello. May I ask your name?"); 
    userName = input.nextLine(); 



    while (answer != 'N') 
    { 
    System.out.println("Welcome " +userName +". Would you like to purchase something? (Y/N) "); 
    answer = input.nextLine().charAt(0); 
    answer = Character.toUpperCase(answer); 

    System.out.println(" ***Lemonaid Stand Program***"); 
    System.out.println("======================================="); 
    System.out.println("Enter purchase item, first letter only."); 
    System.out.println("Lemonaid, Cookies, T-shirt."); 
    System.out.println("Enter Q to Quit."); 
    System.out.println(); 
    System.out.println("Enter an item or Q to quit (L,C,T,Q)"); 
    item = input.nextLine().charAt(0); 
    item = Character.toUpperCase(item); 

    while (item != 'L' && item != 'C' && item != 'T' && item != 'Q') 
    { 
     System.out.println("Invalid input."); 
     System.out.println("Please enter an item, or Q to quit. (L,C,T,Q): "); 
     item = input.nextLine().charAt(0); 
      item = Character.toUpperCase(item);  
    } 

     if (item == 'L') 
     { 
     System.out.println("What size would you like?"); 
     System.out.printf("%-10s%10.2f\n", "12oz.", OZ12_PRICE); 
     System.out.printf("%-10s%10.2f\n", "16oz.", OZ16_PRICE); 
     System.out.println("Input the number only."); 
     lemonaid = input.nextInt(); 

     while (lemonaid != 12 && lemonaid != 16) 

     {System.out.println("I'm sorry. Please only input an 16 or 12."); 
     lemonaid=input.nextInt(); 
     } 
      if (lemonaid == 12) 
      { 
       Sales_Header(); 
      System.out.printf("%-10s%10.2f\n", "12oz.", OZ12_PRICE); 
      } 

      else if (lemonaid == 16) 
      { 
       Sales_Header(); 
      System.out.printf("%-10s%10.2f\n", "16oz.", OZ16_PRICE); 
      } 

      else 
      { 
      } 

     }//end If = L 
     else if (item == 'C') 
     { 
      System.out.println("What type would you like?"); 
      System.out.printf("%-14s%10.2f\n", "Oatmeal", COOKIES_OAT); 
      System.out.printf("%-10s%10.2f\n", "Chocolate Chip", COOKIES_CHOCO); 
      System.out.println("Input the first letter only. (O/C)"); 
      cookies = input.nextLine().charAt(0); 
       cookies = Character.toUpperCase(cookies); 

      while (cookies != 'O' && cookies != 'C') 

      { 
      System.out.println("I'm sorry. Please only input an 'O' or 'C'."); 
      cookies = input.nextLine().charAt(0); 
      cookies = Character.toUpperCase(cookies); 
      } 

      if (cookies == 'O') 
      { 
       Sales_Header(); 
      System.out.printf("%-10s%10.2f\n", "Oatmeal", COOKIES_OAT); 
      } 

      else if (cookies == 'C') 
      { 
        Sales_Header(); 
      System.out.printf("%-10s%10.2f\n", "Chocolate Chip", COOKIES_CHOCO); 
      } 

      else 
      { 
      } 

     }// end else if = C 

     else if (item == 'T') 
      { 
      System.out.println("Would you like that shirt autographed?"); 
      System.out.printf("%-10s%10.2f\n", "With autograph", TSHIRT_Y); 
      System.out.printf("%-10s%11.2f\n", "W/O autograph", TSHIRT_N); 
      System.out.println("Select Yes or No (Y/N)"); 
      tshirt = input.nextLine().charAt(0); 
      tshirt = Character.toUpperCase(tshirt); 

      while (tshirt != 'Y' && tshirt != 'N') 
      { 
       System.out.println("I'm sorry. Please only input an 'Y' or 'N'."); 
       tshirt = input.nextLine().charAt(0); 
       tshirt = Character.toUpperCase(tshirt); 
      } 
       if (tshirt == 'Y') 
       { 
        Sales_Header(); 
       System.out.printf("%-10s%10.2f\n", "With autograph", TSHIRT_Y); 
       } 

       else if (tshirt == 'N') 
       { 
        Sales_Header(); 
       System.out.printf("%-10s%10.2f\n", "W/O autograph", TSHIRT_N); 
       } 

       else 
       { 
       } 
     }//end else if T 

     else if (item == 'Q') 
     { 
      System.out.println("Main Menu."); 
     } 

     else 
     { 
     } 

}//End while loop 
} 

}

답변

0

line을 "먹지"않기 때문에 nextInt() 호출에서 예외가 발생합니다. 나는 당신이 판매 된 아이템의 수를 합계 할 수 있도록 변수를 신속하게 만들었지 만 실제로는 코드에 더 나은 구조가 필요합니다. 설명, 가격 및 판매 수량이 포함될 상품의 수업을 만들 수 있습니다. 응답을 받기 전에 (대답! = N) 수행 중이므로 상단의 while 루프에 문제가 있습니다. 그래서 해결책은 루프 전에 대답을 초기화 한 다음 루프의 끝에서 다시 읽는 것입니다.

import java.util.Scanner; 

public class main { 


    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     String userName = ""; 
     int lemonaid = 0; 
     double OZ12_PRICE = 1.50; 
     double OZ16_PRICE = 2.00; 
     char cookies = 0; 
     double COOKIES_OAT = .75; 
     double COOKIES_CHOCO = .75; 
     char tshirt = 0; 
     double TSHIRT_Y = 15.00; 
     double TSHIRT_N = 8.00; 
     char item; 
     char answer = 0; 

     int numLemonaid = 0; 
     int totalOz = 0; 
     int numCookies = 0; 
     int numTShirts = 0; 

     System.out.println("Hello. May I ask your name?"); 
     userName = input.nextLine(); 

     System.out.println("Welcome " + userName + ". Would you like to purchase something? (Y/N) "); 
     answer = input.nextLine().charAt(0); 
     answer = Character.toUpperCase(answer); 

     while (answer != 'N') { 
      System.out.println(" ***Lemonaid Stand Program***"); 
      System.out.println("======================================="); 
      System.out.println("Enter purchase item, first letter only."); 
      System.out.println("Lemonaid, Cookies, T-shirt."); 
      System.out.println("Enter Q to Quit."); 
      System.out.println(); 
      System.out.println("Enter an item or Q to quit (L,C,T,Q)"); 
      item = input.nextLine().charAt(0); 
      item = Character.toUpperCase(item); 

      while (item != 'L' && item != 'C' && item != 'T' && item != 'Q') { 
       System.out.println("Invalid input."); 
       System.out.println("Please enter an item, or Q to quit. (L,C,T,Q): "); 
       item = input.nextLine().charAt(0); 
       item = Character.toUpperCase(item); 
      } 

      if (item == 'L') { 
       System.out.println("What size would you like?"); 
       System.out.printf("%-10s%10.2f\n", "12oz.", OZ12_PRICE); 
       System.out.printf("%-10s%10.2f\n", "16oz.", OZ16_PRICE); 
       System.out.println("Input the number only."); 
       lemonaid = input.nextInt(); 
       input.nextLine(); 

       numLemonaid++; 

       while (lemonaid != 12 && lemonaid != 16) 

       { 
        System.out.println("I'm sorry. Please only input an 16 or 12."); 
        lemonaid = input.nextInt(); 
        input.nextLine(); 
       } 
       totalOz += lemonaid; 
       if (lemonaid == 12) { 
        // Sales_Header(); 
        System.out.printf("%-10s%10.2f\n", "12oz.", OZ12_PRICE); 
       } 

       else if (lemonaid == 16) { 
        /// Sales_Header(); 
        System.out.printf("%-10s%10.2f\n", "16oz.", OZ16_PRICE); 
       } 

       else { 
       } 

      } // end If = L 
      else if (item == 'C') { 
       System.out.println("What type would you like?"); 
       System.out.printf("%-14s%10.2f\n", "Oatmeal", COOKIES_OAT); 
       System.out.printf("%-10s%10.2f\n", "Chocolate Chip", COOKIES_CHOCO); 
       System.out.println("Input the first letter only. (O/C)"); 
       cookies = input.nextLine().charAt(0); 
       cookies = Character.toUpperCase(cookies); 
       numCookies++; 

       while (cookies != 'O' && cookies != 'C') 

       { 
        System.out.println("I'm sorry. Please only input an 'O' or 'C'."); 
        cookies = input.nextLine().charAt(0); 
        cookies = Character.toUpperCase(cookies); 
       } 

       if (cookies == 'O') { 
        // Sales_Header(); 
        System.out.printf("%-10s%10.2f\n", "Oatmeal", COOKIES_OAT); 
       } 

       else if (cookies == 'C') { 
        // Sales_Header(); 
        System.out.printf("%-10s%10.2f\n", "Chocolate Chip", COOKIES_CHOCO); 
       } 

       else { 
       } 

      } // end else if = C 

      else if (item == 'T') { 
       System.out.println("Would you like that shirt autographed?"); 
       System.out.printf("%-10s%10.2f\n", "With autograph", TSHIRT_Y); 
       System.out.printf("%-10s%11.2f\n", "W/O autograph", TSHIRT_N); 
       System.out.println("Select Yes or No (Y/N)"); 
       tshirt = input.nextLine().charAt(0); 
       tshirt = Character.toUpperCase(tshirt); 

       numTShirts++ ; 

       while (tshirt != 'Y' && tshirt != 'N') { 
        System.out.println("I'm sorry. Please only input an 'Y' or 'N'."); 
        tshirt = input.nextLine().charAt(0); 
        tshirt = Character.toUpperCase(tshirt); 
       } 
       if (tshirt == 'Y') { 
        // Sales_Header(); 
        System.out.printf("%-10s%10.2f\n", "With autograph", TSHIRT_Y); 
       } 

       else if (tshirt == 'N') { 
        // Sales_Header(); 
        System.out.printf("%-10s%10.2f\n", "W/O autograph", TSHIRT_N); 
       } 

       else { 
       } 
      } // end else if T 

      else if (item == 'Q') { 
       System.out.println("Main Menu."); 

      } 

      else { 
      } 

      System.out.println("Welcome " + userName + ". Would you like to purchase something? (Y/N) "); 
      answer = input.nextLine().charAt(0); 
      answer = Character.toUpperCase(answer); 
     } // end while 

     System.out.println("Lemonaid: " + numLemonaid + "(" + totalOz +"Oz)") ; 
     System.out.println("Shirts: " + numTShirts) ; 
     System.out.println("Cookies: " + numCookies) ; 
    } 
} 
+0

이것은 큰 도움이됩니다! 시간 내 주셔서 대단히 감사합니다. – ArchAngel

1

표준 입력 (A/K/A STDIN의 A/K/AA 키보드)에서 단일 문자를 얻으려면, 당신은 두 가지 방법 중 하나를 수행 할 수 있습니다

char tshirt = (char) System.in.read(); //Takes one character, casts to "char" 

또는 스캐너의 메소드를 다음과 같이 사용할 수 있습니다.

이렇게하면을 처리하지 않아도됩니다.오류.

관련 문제