Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,109 members, 7,957,150 topics. Date: Tuesday, 24 September 2024 at 08:03 AM |
Nairaland Forum / Science/Technology / Programming / I Am Having Error With This Code Incompatible Type Error(SOLVE) (1064 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)
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 |
Re: I Am Having Error With This Code Incompatible Type Error(SOLVE) by asalimpo(m): 11:55pm On Dec 09, 2017 |
klazbaba: ^^^ |
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)
Developer Needed For 3 Days Work Pay $150 / About The Best web design Company in Port Harcourt Boxvibe.com / List Of All Blockchain Training Centers Across All States In Nigeria
(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. 9 |