Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,466 members, 7,808,665 topics. Date: Thursday, 25 April 2024 at 03:07 PM

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

Nairaland Forum / Science/Technology / Programming / I Am Having Error With This Code Incompatible Type Error(SOLVE) (1036 Views)

I Am Bored, I Just Tried This Code / I Am New To C Programming, I Am Having Issue With This Code / Java Programmers ,I Need Help Fixing This Code. (2) (3) (4)

(1) (Reply) (Go Down)

I Am Having Error With This Code Incompatible Type Error(SOLVE) by oloyemeji: 11:36am On Dec 09, 2017
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: 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: 11:22pm On Dec 09, 2017
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: 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)

Zuckerberg Deflects US Senators’ Facebook Questions, And Gets $3 Billion / 10 Programming Languages/ Tools For Kids / Blockchain And The Evolution Of Programmatic Advertising – A 2020 Perspective

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.