-1

我正在尝试从文件中读取数据并将数据存储在三个整数中,红色、绿色和蓝色文件中的数据设置为:

255,0,0
0,255,0
0,0,255
0,255, 255

我已经尝试过这样做的方法,但我没有成功任何人都可以帮助这里是迄今为止该方法的副本:

private void readColoursFromFile()  {

    int red = 128;
    int green = 128;
    int blue = 128;

    StringBuilder contents = new StringBuilder();
    String filename = "colours.txt";
     try { 
    BufferedReader reader;

   reader = new BufferedReader(new FileReader(filename));
   String str;

   while (( str = reader.readLine()) != null) {

       StringTokenizer tokenizer = new StringTokenizer(str, ",");
       String rcolour = tokenizer.nextToken(); 
       String gcolour = tokenizer.nextToken(); 
       String bcolour = tokenizer.nextToken(); 
       Integer irColour = red;
       Integer igColour = green;
       Integer ibColour = blue;
       rcolour = Integer.toString(irColour);
       gcolour = Integer.toString(ibColour);
       bcolour = Integer.toString(igColour);
     contents.append(str);
       printColourRow(red, green, blue);    
       }
   {
     reader.close();
  }
   } catch (IOException e) {
    System.out.println("error");
    e.printStackTrace();
}
}
4

2 回答 2

1

代替

   Integer irColour = red;
   Integer igColour = green;
   Integer ibColour = blue;
   rcolour = Integer.toString(irColour);
   gcolour = Integer.toString(ibColour);
   bcolour = Integer.toString(igColour);

   red = Integer.parseInt(rcolour); 
   green = Integer.parseInt(gcolour); 
   blue = Integer.parseInt(bcolour);
于 2012-04-06T19:37:05.190 回答
0

您正在从此文件中设置rcolourgcolour和值,但您没有对它们做任何事情。bcolour然后将这些值设置为 128。然后打印颜色行(使用 printColourRow)值 128,128,128。变量red,greenblue在顶部都设置为 128 并且永远不会更改。

你不应该打印出你从文件中读取的内容吗?如:

printColourRow(Integer.parseInt(rcolour),
               Integer.parseInt(gcolour),
               Integer.parseInt(bcolour));
于 2012-04-06T19:35:45.927 回答