473,397 Members | 2,099 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,397 software developers and data experts.

why does the following program takes only single digit input ?

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. class D31
  3. {
  4.     public static void main (String args[]) throws IOException
  5.     {
  6.         char c[] = new char[20];
  7.         char d[] = new char[20];
  8.         char temp;
  9.         char i,j;
  10.  
  11.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12.         BufferedReader cr = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14.         System.out.println("enter 5 elements");
  15.  
  16.         for(i=0;i<5;i++)
  17.         {
  18.             c[i] = (char)  br.read();
  19.             System.out.println(c[i]);
  20.         }
  21.  
  22.         System.out.println("enter 5 elements");
  23.  
  24.         for( i=0;i<5;i++)
  25.         {
  26.             d[i] = (char)  cr.read();
  27.             System.out.println(d[i]);
  28.         }
  29.  
  30.         for( j=0;j<5;j++)
  31.         {
  32.             c[i]=d[j];
  33.             i++;
  34.         }    
  35.  
  36.  
  37.         System.out.println("sorted array");
  38.  
  39.         for(i=0; i<10; i++)
  40.         {
  41.             for(j=i; j< 9; j++)
  42.                 {
  43.                 if(c[i]< c[ j+1])
  44.                         {
  45.                         temp=c[i];
  46.                             c[i]=c[ j+1];
  47.                             c[ j+1]=temp;
  48.                         }
  49.                 }
  50.         }
  51.  
  52.  
  53.  
  54.         for(i=0;i<10;i++)
  55.         {
  56.             System.out.print("\t"+c[i]);    
  57.         }
  58.  
  59.     }
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
Feb 3 '14 #1

✓ answered by Nepomuk

Hi learner12 and welcome to bytes.com!

The BufferdReader.read() function only reads one character, so you will only get one digit of your input. You could use the BufferedReader.readLine() method if the whole line will do; for more advanced stuff, use a Scanner rather than a BufferedReader. That offers many useful methods for reading data.

1 1187
Nepomuk
3,112 Expert 2GB
Hi learner12 and welcome to bytes.com!

The BufferdReader.read() function only reads one character, so you will only get one digit of your input. You could use the BufferedReader.readLine() method if the whole line will do; for more advanced stuff, use a Scanner rather than a BufferedReader. That offers many useful methods for reading data.
Feb 3 '14 #2

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

Similar topics

2
by: RadiationX | last post by:
How does this program work? // fig06_07.c -- COP2220 Example -- 040209 (sjd) // Student poll program #include <stdio.h> #include <stdlib.h> #define RESPONSE_SIZE 40 //...
3
by: gigs68 | last post by:
What script would I need to write (vb.net) to validate a credit card number's length and first digit input to a text box (i.e., Visa Length=16 digits, first digit starts with a 4)? Thanks Bryan
2
by: Zytan | last post by:
This page http://blog.stevex.net/index.php/string-formatting-in-csharp/ shows: {0:0,0} but for single digit numbers, it prefixes a 0! Is there a way that works with single digit numbers? thanks...
1
by: aheine | last post by:
Hey! So i have a program i need to write that must include the user to input their birthdate (mm-dd-yyyy) . Then i must validate the input from the user, meaning i need to check to make sure the...
3
by: Frank Rizzo | last post by:
Hello, I am trying to print out an hour in a format of 3pm or 5am. I've tried the following, but it throws an exception saying invalid input string: string times =...
1
by: tg | last post by:
http://img522.imageshack.us/img522/8647/scan10005ci7.jpg As a percentage of world inhabitants, the white population will plummet to a single digit (9.76%) by 2060 from a high-water mark of...
1
by: surendrasharma86 | last post by:
hello , i have an assignment that is find the single digit sum of any given number. It have some instructions:- 1) No looping use. 2) No conditional...
20
by: madhuriks | last post by:
hi gud mrng, in my form....i need to take either single digit in textbox i.e., '1' or '12'..what i did is as follows login.jsp <td><font color="#800000" size=""><b>Alert-Msg-Time (in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.