₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,324,999 members, 8,419,863 topics. Date: Thursday, 04 June 2026 at 04:26 AM

Toggle theme

I Am Having Error With This Code Incompatible Type Error(SOLVE) - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingI Am Having Error With This Code Incompatible Type Error(SOLVE) (1142 Views)

1 Reply (Go Down)

I Am Having Error With This Code Incompatible Type Error(SOLVE) by oloyemeji(op):
Java Program Example - Reverse an Array */

import java.util.Scanner;

public class JavaProgram
{
public static void main(String args[])
{
float size, i, j, temp;
float arr[] = new float[50];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Array Size : " );
size = scan.nextFloat();

System.out.print("Enter Array Elements : " );
for(i=0; i<size; i++)
{
arr[i] = scan.nextFloat();
}

j = i - 1; // now j will point to the last element
i = 0; // and i will point to the first element

while(i<j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}

System.out.print("Now the Reverse of Array is : \n" );
for(i=0; i<size; i++)
{
System.out.print(arr[i]+ " " );
}
}
} //please help me correct this error its displaying error incompatible type : float can't be convert to int, when I use Int it works but in this situation I am dealing with float.
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by Dreadlord: 5:50pm On Dec 09, 2017
System.out.print("Enter Array Size : " );
change this size = scan.nextFloat();
to size = scan.nesting();
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by Dreadlord: 5:51pm On Dec 09, 2017
System.out.print("Enter Array Size : " );
change this size = scan.nextFloat();
to size = scan.nextInt(); you can not have array of size 2.5.
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by oloyemeji(op): 7:32pm On Dec 09, 2017
The error start from here. arr[i] = scan.nextFloat();
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by klazbaba(m): 11:13pm On Dec 09, 2017
float size, i, j, temp;

Make your i an integer. For when you get here:
arr[i] = scan.nextFloat();

arr[1.1] = scan.nextFloat(); would be likely. And that would be out flat wrong. Since array indices can only be integers.

Just as an addition, also make size an integer. Though, it wouldn't throw an exception, but don't make it a floating point number.
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by HealerC:
cool
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by asalimpo(m): 11:55pm On Dec 09, 2017
klazbaba:
float size, i, j, temp;


... also make size an integer. ...
^^^
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by oloyemeji(op): 10:25am On Dec 12, 2017
It work now, I declare everything as integer, except this. temp which is float and this still remain float arr[] = new float[50];
1 Reply

As A Programmer, What Will Be The Output Of This Code?I Am Bored, I Just Tried This CodeJava Programmers ,I Need Help Fixing This Code.234

Simple Way To Develop Android AppsLearn Codeigniter Step By Step From Me - AffordableHelp Review My Android App