Log in

View Full Version : Char input problems


chialin
Oct 8, 2012, 01:51 AM
Why when I try to compile , it says "variable ch is already defined in method main(java.lang.String[])"? I have probkems getting char inputs.

import java.util.Scanner;



public class Tolak{
public static void main(String [] args) {
int correct=0;
char ch;

Scanner input= new Scanner(System.in);

System.out.print("You have RM10. After buying a drink that costs RM4.50, how much you left?\n");
System.out.print("A:RM6.50\n");
System.out.print("B:RM5.50\n");
char ch=(sc.next).charAt(0);
System.out.println(ch);
if (ch=='a');
correct += 1;

System.out.print("Marks you get = "+ correct);
}


}

rameshspry
Jul 7, 2013, 09:52 PM
the ch is already declared

and the reading input syntax is also wrongly arranged

it should be like this...


import java.util.Scanner;



public class Tolak{
public static void main(String [] args) {
int correct=0;
char ch;

Scanner input= new Scanner(System.in);

System.out.print("You have RM10. After buying a drink that costs RM4.50, how much you left?\n");
System.out.print("A:RM6.50\n");
System.out.print("B:RM5.50\n");
ch=input.next().charAt(0);;
System.out.println(ch);
if (ch=='a');
correct += 1;

System.out.print("Marks you get = "+ correct);
}


}