Results 1 to 5 of 5
  1. #1
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default Your variable is never read

    Hi, I am not sure whether I have to put the scanner class in main function or some other part of prog.
    My code is as follows , it doesn't work since
    Java Code:
    char ch = read.charAt(0);
    is never read.
    My code is as follows:
    Java Code:
    import java.util.Scanner;
    public class calculator {
     int x, y ;
        
    	
    	int calc() {
    		
    		int sum = x+y;
    		return sum;
    		
    	}
    			
    		public static void main(String args[]) {
    			Scanner input = new Scanner(System.in);
    			String read = input.nextLine();
    			char ch = read.charAt(0);	
    			calculator c = new calculator();
    			
    			System.out.println("Enter The First No.");
    			c.x = input.nextInt();
    			System.out.println("Enter The Second  No.");
    			c.y = input.nextInt();
    			
    			System.out.println("The sum is " +c.calc());
    			
    		}
    		
    	}
    Please give me some guidance regarding scanner classes as to what is its exact position in program classes or functions or main classes.
    If I run the program I dont get any output.
    Thanks for the help.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Just get rid of it then:
    Java Code:
    public static void main(String args[]) {
    		Scanner input = new Scanner(System.in);
    		calculator c = new calculator();
    			
    		System.out.println("Enter The First No.");
    		c.x = input.nextInt();
    		System.out.println("Enter The Second  No.");
    		c.y = input.nextInt();
    		
    		System.out.println("The sum is " +c.calc());
    		
    }

  3. #3
    JavaVideos is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    You say you don't get any output, but if I run it I get some output.

    I think the problem is that you are reading three lines, whereas you only want to read 2.

    The first line you don't really do anything with ( apart from assign the first character to the variable ch).

    the second and third lines are read in integers, so if you type in integers they are handled correctly and the sum of them is printed out:

    The output from your code, from me entering 1, 1, 2:
    Java Code:
    1
    Enter The First No.
    1
    Enter The Second  No.
    2
    The sum is 3

  4. #4
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Actually I want to continue the program to run as long as user enters Y and should stop if user enters N, that is why I had used chartAt() initialy.
    Removing charAt() makes the prog to run only for one case.

  5. #5
    JavaVideos is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    At the moment the program starts and the beginning, executes each line of code in turn then finishes.

    There is nothing there to tell it to repeat. I would recommend using a while loop for that.

Similar Threads

  1. how we can get the count value in a variable
    By arunkumarinfo in forum JDBC
    Replies: 4
    Last Post: 01-23-2009, 10:39 PM
  2. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  3. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 02:42 AM
  4. Getting variable value from a variable name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:26 PM
  5. volatile variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •