473,426 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

what is the problem in the following code?

108 100+
# import java.io.IOException;
#
# class Toread
# {
# public static void main(String args[])
# {
# int x;
# System.out.println("Enter a digit");
# try
# {
# x= System.in.read();
# System.out.println("Entered Value:"+x);
# }
# catch(IOException e)
# {
# System.out.println("You had entred wrong value");
# }
# }
#
# }

When I try to read a value and print it its not projecting the same i dont understand what is the mistake i am giving the code which i tried to compile

When i try to compile the output is this way
Enter a digit: 1
Entered Value :50
Jul 14 '10 #1
3 1326
um, i have no idea why its giving you the answer it is. i tried your code out for myself inputting 12 and the program would echo 49.

i have always been shown to grab user input using the Scanner class and creating a scanner object
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class ToRead {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner kb = new Scanner(System.in);
  7.         int x;
  8.         System.out.println("Enter a digit");
  9.         try {
  10.             x = kb.nextInt();
  11.             System.out.println("Entered Value:" + x);
  12.         } catch (Exception e) {
  13.             System.out.println("You had entred wrong value");
  14.         }
  15.     }
  16. }
  17.  
  18.  
now, when i try to input 12, it echos the inputted number, 12.

You should check out the scanner class, it's quite useful and has lots of features for grabbing user input from keyboard, etc. etc.

Java Scanner Class

hope this helps :)
Jul 14 '10 #2
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4.  
  5. public class SysRead {
  6.     public static void main(String[] args) { 
  7.         String x; 
  8.         System.out.println("Enter a digit"); 
  9.         try { 
  10.             BufferedReader x1 = new BufferedReader(new InputStreamReader(System.in));
  11.             System.out.println("Entered Value:" + Integer.parseInt(x1.readLine())); 
  12.         } catch (Exception e) { 
  13.             System.out.println("You had entred wrong value"); 
  14.         } 
  15.     } 
  16. }
Try this out.... hope ul understand the difference
Jul 15 '10 #3
Oralloy
988 Expert 512MB
BTW, 50 is the (decimal) ASCII value for '2'. Check your input once again.

What your program is doing is reading the raw byte, hoisting that value to an integer, and then string converting the integer to generate your output.

Your code likely would have worked if you'd declared 'x' as a char.
Jul 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marlon | last post by:
<! -- Web.config--> <authentication mode="Windows" /> <identity impersonate="true" /> <! -- Web.config--> where Configurations.ADsUsersPath is...
3
by: Razvan | last post by:
Hi! What is wrong with the following code ? // Test1.cpp : Defines the entry point for the console application. //
3
by: Materialised | last post by:
I am having some issues with the following code, basically what I am trying to do, is familierise myself with compiler warnings, when I try to access a private or protected class function. Here is...
0
by: Just Me | last post by:
What's wrong with the following code? The TempName read is valid since I just wrote it and also did a QuickWatch to check it. But once I set the PrinterName the count of papersizes goes to zero. ...
30
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
4
by: -D- | last post by:
I'm new to .net and just learning c#. I have the following code in vb and want to convert this to c# Public Class PositionData Private strText As String Private strUrl As String Public...
7
by: Jason Kid | last post by:
Hi, Please tell me what's wrong with the following code. private bool CheckIt( int iArg, bool fArg) { bool fIsGood = false;
2
by: SRT | last post by:
Hi, I have the following code below, which works if I do the following Call No1 'Call No2 Exit Sub For whatever reason it fails at Me!.SetFocus at the end of Sub No2 with
6
by: CSharper | last post by:
I have following code, if you cut and paste it in VS and compile it you see only an answer of 0. I was expecting values from o through 2999 using System; using System.Collections.Generic;...
3
by: vrsathyan | last post by:
Hi.., While executing the following code in purifier.., std::vector<int> vecX; vecX.clear(); int iCount = 0; { int iVal;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.