Ask Experts Questions for FREE Help !
Ask
    chialin's Avatar
    chialin Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 8, 2012, 01:51 AM
    Char input problems
    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's Avatar
    rameshspry Posts: 4, Reputation: 1
    New Member
     
    #2

    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);
    }


    }

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Char[] array insert [ 0 Answers ]

Hey I was just wondering how I would solve this, I have tried many options but none of them seem to work, I have commented in what I actually need done, would someone please be able to help me? Public class StringImproved { char content; public boolean insert(java.lang.String...

Hi, input problems... [ 1 Answers ]

I try to type and got wrong input, I change time region... is all usa but I have to hold fn blue key down to write correct. Exam*le... like= 352e

Using C++ (if, char or Boolean) [ 0 Answers ]

Using C++ (if, char or Boolean) Write a program that asks the user for a two digit number and then prints the English word for that number. Sample run: Enter a two-digit number: 45 You entered the number forty-five. PLAN: Read the number entered as an integer and then break the number up...


View more questions Search