This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

problem in loop  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am just doing one simple program in that i am giving input from console but it gives me random output.please help me
thanks in advance
the code is :::
import java.io.*;
import java.util.*;
import java.lang.*;

class Array
{
public static void main(String args[])
throws IOException
{
int number;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int array[]=new int[10];
for(int i=0;i<=2;i++)
{
number = (int) br.read();
array[i]=number;
}
for(int i=0;i<10;i++)
{
System.out.print(array[i]+" ");
System.out.println(" ");
}
Arrays.sort(array);
}
}


it will give random output
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Not sure what you mean by "random output," but note that this program will print the character codes of the characters you type in -- not the interpretation of those codes as textual integers. The number "48" is the character code for 0; 49 is 1; etc.

The proper way to write your program is to read Strings, then use the static Integer.parseInt() method to interpret those strings. See Sun's I/O tutorial at http://java.sun.com/docs/books/tutorial/essential/io/index.html .
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!