I'm trying to make an array for ParkingArea(string, int, int, int, int). The file that is getting scanned is here http://uwf.edu/jcoffey/data.txt
try
{
Scanner scan = new Scanner(file);
for (int i = 0;scan.hasNext(); i++)
{
for(int x = 0; x < 4; x++)
{
if(x == 0)
{
name = scan.next();
}
else
{
array[x-1] = scan.next();
}
}
ParkingArea[i] = new ParkingArea(name, array[0], array[1], array[2], array[3]);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
I get this error when I compile.
Parking.java:40: error: incompatible types
array[x-1] = scan.next();
^
Any help would be GREATLY appreciated.
scan.next() returns a String. If you want to convert it to an int, you need to call `
Integer.parseInt(scan.next())
Instead of
array[x-1] = scan.next();
give
array[x-1] = scan.nextInt();
a try. Assuming that array
is of type int[]
, then your issue comes from assigning the int
value at array[x-1]
to a String
, which is what scan.next()
returns. Using scan.nextInt()
will return an int
, thus solving the error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.