2014-10-22 8 views
1

내 프로그램은 파일을 가져 와서 파일의 특정 부분을 다른 배열에 추가합니다. 여기 내 코드입니다 :배열의 특정 부분 추가 ​​

public class GradeBookApp { 

public static void main(String[] args) throws IOException { 

    String fileName = ""; 
    String name = ""; 
    char[] categoryCodes = new char[5]; 
    String[] categories = new String[5]; 
    double[] categoryWeights = new double[5]; 
    double[][] gradeTable; 
    GradeBook myGB = new GradeBook (name, categoryCodes, 
    categories, categoryWeights); 


    if (args.length > 0) { 

    for (int i = 0; i < args.length; i++) { 
     System.out.println("File \"" + args[i] 
      + "\" read in and Gradebook object created."); 

     fileName = args[i]; 
     Scanner scanFile = new Scanner(new File(fileName)); 

     name = scanFile.nextLine(); 
     int numOfCodes = scanFile.nextLine(); 

     for (i = 0; i < 5; i++) { 
      categoryCodes = scanFile.nextLine().substring(0).toCharArray(); 
     } 
    } 
} 

그리고 여기에 파일입니다

Student1
5
A를 0.05
Q 퀴즈 0.10
p는 0.25
전자 시험 프로젝트 관리 0.30
F 결승 0.30

a100 a95 a100 a100 a100 F92

0 q90의 Q80 Q100의 Q80의 Q80의 R90
P100 P95의 P100의 P85의 P100
e77.5 E88 나는 배열을 분리 할 수있는 굵은 코드를 추가하려고 해요. 편지는 categoryCodes에 들어가야하며, 단어는 카테고리에 들어가야하며, 숫자는 카테고리 가중치로 들어가야합니다. 나는 그것의 각 배열에 첫 번째 부분을 추가하려고 시도했지만 올바르게했는지 확신 할 수 없다. 또한 올바른 배열에 해당 줄의 두 번째 및 세 번째 부분을 추가하는 방법을 잘 모르겠습니다.

답변

0

split() 방법을 사용할 수 있습니다.

for (i = 0; i < 5; i++) { 
     String[] all = scanFile.nextLine().split(" "); 
     if(all.length == 3 && all[0].length() == 1 && all[2].matches("(\\d+\\.\\d+)")){ 
      categoryCodes[i] = all[0].charAt(0); 
      categories[i] = all[1]; 
      categoryWeights[i] = Double.parseDouble(all[2]); 
     } 
    } 
+0

고마워요! categoryCodes의 파트는 완벽하게 작동하지만 두 번째 파트는 [Ljava.lang.String; @ 7ea987ac 및 [D @ 7ea987ac. 이 문제를 어떻게 해결할 수 있습니까? – me123

+0

@ me123 배열을 보려면 다음을 사용하십시오 :'System.out.println (Arrays.toString (categories));'categoryWeights'에 대해 같은 작업을하십시오. – Titus

0

내 이해 당으로 입력하는 경우 : -

a Activities 0.05 

할당 등이 될 수 있어야합니다 -

categoryCodes[0] = 'a'; 
categories[0] = "Activities"; 
categoryWeights[0] = 0.05; 

지금 a, Activities, 0.05의 인덱스 인수에 0, 12 경우 사용할 수있는 배열

 for(int i = 0; i < arrg.length; i++){ 
     if(i %3 == 0){ 
      //this would be char a 
     }else if(i %3 == 1){ 
       // this would be your string part 
     }else{ 
      //i %3 == 2 this would be your number part 
     } 
    } 

if else 대신 switch 문을 사용할 수 있습니다.