Results 1 to 5 of 5
Thread: Your variable is never read
- 09-09-2009, 12:34 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
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 sinceis never read.Java Code:char ch = read.charAt(0);
My code is as follows:
Please give me some guidance regarding scanner classes as to what is its exact position in program classes or functions or main classes.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()); } }
If I run the program I dont get any output.
Thanks for the help.
- 09-09-2009, 12:40 PM #2
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()); }
- 09-09-2009, 05:42 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
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
- 09-10-2009, 12:05 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
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.
- 09-10-2009, 12:13 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
how we can get the count value in a variable
By arunkumarinfo in forum JDBCReplies: 4Last Post: 01-23-2009, 09:39 PM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
getting the value of variable
By Lehane_9 in forum New To JavaReplies: 2Last Post: 03-05-2008, 01:42 AM -
Getting variable value from a variable name
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:26 PM -
volatile variable
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks