Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Error in program

  Asked By: Everett    Date: Jun 06    Category: Java    Views: 657
  

import java.io.*;
public class first {
public static void main(String args[]) throws java.io.IOException
{
int i=0, j=0;

int a[][]= new int[4][4];
for ( i=0;i<a.length;i++)
{
for(j=0;j<a.length;j++)
a[i][j]= System.in.read();
}
System.out.print(" the a[0][0] = " + a[0][0] );
System.out.print(" the a[1][1] = " + a[1][1] );
}
}

can any one tell me what is the problem in this program.plz.
i can only enter 6 element. (not 16)
did any one know how to enter any thing from keyboard( the commands).thank you .


<HTML><BODY bgcolor="#FFFFFF"><a Target="_top"
href="http://www.flamingtext.com/ymail.html" ><img
src="ymail.flamingtext.com/.../flamingtext_com_1051618828_32\
565.gif" border=0 alt="Image by FlamingText.com"></a>
<br>Image by <a
href="http://www.flamingtext.com/ymail.html"
>FlamingText.com</a>
</BODY></HTML>

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Jean Bell     Answered On: Jun 06

I dont know what the exact problem is but using bufferedReaders
instead of System.in.read() and conversion to int  made your program
execute correctly.

import java.io.*;
public class  first {
public static  void main(String args[]) throws  java.io.IOException
{
int i=0, j=0;
BufferedReader stdin=new BufferedReader(new
InputStreamReader(System.in));
int a[][]= new int[4][4];
for ( i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print("enter:");
a[i][j]=
Integer.parseInt(stdin.readLine());

}

}
System.out.println(" the a[0][0] = " + a[0][0] );
System.out.println(" the a[1][1] = " + a[1][1] );
}
}

 
Answer #2    Answered By: Dominic Murphy     Answered On: Jun 06

Look here 123

ahA 4
9
153
8rD
the a[0][0] = 97
the a[0][1] = 104
the a[0][2] = 65
the a[0][3] = 32
the a[1][0] = 52
the a[1][1] = 13
the a[1][2] = 10
the a[1][3] = 57
the a[2][0] = 13
the a[2][1] = 10
the a[2][2] = 49
the a[2][3] = 53
the a[3][0] = 51
the a[3][1] = 13
the a[3][2] = 10
the a[3][3] = 56

Now , step by step.

What does it mean System.in.read() that means it returns you every
character in bytes .

The char value 'a' equals 97 in bytes
'h' = 104, 'A' = 65 , Whitespace = 32, '4' = 52, enter = 13 and 10
(end of line depends from system), '9' = 57 and so on

characters of 0..9 , a..z, A..Z are reserved in range of (48 - 57
), (97 - 122), (65 - 90)
comma = 44, dot = 46 and so on

Use readLine() or read(byte[] b, int  start, int len) functions Or
do casting like (char)System.in.read();.

 
Didn't find what you were looking for? Find more on Error in program Or get search suggestion and latest updates.




Tagged: