次のようなテキスト ファイルを読み込もうとしています。
Cycle [numberOfWheels=4.0, weight=1500.0]
コードは正しく実行されますが、double は認識されません。出力は次のとおりCycle [numberOfWheels= 0.0, weight= 0.0]
です。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Cycle {
public static double numberOfWheels;
public static double weight;
public Cycle(double numberOfWheels, double weight)
{
this.numberOfWheels=numberOfWheels;
this.weight=weight;
}
@Override
public String toString() {
return "Cycle [numberOfWheels= " + numberOfWheels + ", weight= " + weight
+ "]";
}
public static void main(String[] args) throws FileNotFoundException
{
// TODO Auto-generated method stub
File text = new File("C:/Users/My/workspace/CycleOutput/Cycle.txt");
try {
Scanner sc = new Scanner(text);
while (sc.hasNextDouble())
{
numberOfWheels=sc.nextDouble();
}
while (sc.hasNextDouble())
{
sc.next();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
Cycle cycle1=new Cycle(numberOfWheels, weight);
System.out.println(cycle1.toString());
}
}