string cannot be converted to int! how to sum a scanned int on java? -
i wrote simple programme sum scanned int written user, when compile it, says "string cannot converted int." wrong in program?
class="snippet-code-html lang-html prettyprint-override">import java.util.*; public class pr6{ public static void main(string[] args){ scanner scan = new scanner (system.in); int num1; int num2; int num3; int sum; system.out.print("please write integer: "); num1 = scan.nextline(); system.out.print("please write integer: "); num2 = scan.nextline(); system.out.print("please write integer: "); num3 = scan.nextline(); sum = num1 + num2 + num3; system.out.print("total = " + sum); }//main }//pr6
here issue
num1 = scan.nextline();
let's @ info type num1 is:
int num1;
scan.nextline()
homecoming string. , can't have int num1 = "1", because different info types.
you should utilize scan.nextint()
. homecoming number. that'll solve problems :)
so you'll have num1 = scan.nextint()
, num2 = scan.nextint()
, , num3 = scan.nextint()
.
i hope helps. luck!
java string int sum
No comments:
Post a Comment