Our teacher really sux, he just wrote this down on the board but didn't explain the program :(...after a lot of search i finally understand what is try and catch but what is the meaning of byte b=new byte[255]; in this program ? and why the program is not working ??

class Stu{
private int rollno,semester;
private String name;

    
protected String input(){
    int l=0;
    byte b=new byte[255]; // what is this
    String S;
    try{ 
    l=System.in.read(b,0,255); // i know what is try and catch but what is (b,0,255) ?
    }
    catch (Exception r){} // nothing in the catch :(
    S=new String (b,0,l-2); // what is l-2 ??
    return S;
    }

protected void get(){
    System.out.print ("Enter name:");
    name=input();
    System.out.print ("Enter Roll number: ");
    rollno=Integer.parseInt(input());
    System.out.print("Enter Semester: ");
    semester=Integer.parseInt(input());
    }
    
protected void show(){
    System.out.print(name);
    System.out.print(rollno);
    }
}

class Stu1 extends Stu{
    private int marks,tech;
    
    protected void getdata(){
    super.get();
    tech=Integer.parseInt(super.input());
    marks=Integer.parseInt(super.input());
    }
    
    protected void showdata(){
    super.show();
    System.out.println ("Technology: " + tech);
    System.out.println ("Marks: " + marks);
    }
}

class Stu1demo {
public static void main(String []args){
    Stu1 S1 = new Stu1();
    S1.getdata();
    S1.showdata();
    }
}

Errors:

Stu1demo.java:8: incompatible types
found : byte[]
required: byte
byte b=new byte[255];
^
Stu1demo.java:11: cannot find symbol
symbol : method read(byte,int,int)
location: class java.io.InputStream
l=System.in.read(b,0,255);
^
Stu1demo.java:14: cannot find symbol
symbol : constructor String(byte,int,int)
location: class java.lang.String
S=new String (b,0,l-2);
^
3 errors

It's a mistake. It should read
byte[] b=new byte[255];
byte is a numeric type - an 8 bit integer. This declares an array of 255 bytes.
Because b was incorrectly declared, the methods that have it as a parameter also don't compile. The method that was supposed to be used was

public int read(byte[] b, int off, int len) throws IOException
Reads up to len bytes of data from the input stream into an array of bytes.

If you fix the declaration the read methods should be OK.

Incidentally the use of l as a variable name, especially for a number, is sheer lunacy. Line 14 is l (ie the variable) -2, but it looks like 1 (the number) -2, and baffles everybody. The color-coded syntax in the Daniweb code tags is the only thing making this obvious.

Comments
thanksalot :)

You program is not working because you/your teacher have used the wrong syntax for declaring the byte array.
It should be like this

byte[] b = new byte[255];

it means declare an array object b of type byte and it can store 255 variables of type byte.

Comments
thanks

It's a mistake. It should read
byte[] b=new byte[255];
byte is a numeric type - an 8 bit integer. This declares an array of 255 bytes.
Because b was incorrectly declared, the methods that have it as a parameter also don't compile. The method that was supposed to be used was

If you fix the declaration the read methods should be OK.

Incidentally the use of l as a variable name, especially for a number, is sheer lunacy. Line 14 is l (ie the variable) -2, but it looks like 1 (the number) -2, and baffles everybody. The color-coded syntax in the Daniweb code tags is the only thing making this obvious.

thanks alot..! the program is working now,..!

protected String input(){
	int l=0;
	byte[] b=new byte[255];
	String S;
	try{
	l=System.in.read(b,0,255);
	}
	catch (Exception r){}
	S=new String (b,0,l-2);
	return S;
	}

i've understand this is the method for taking user input!! but why l is declared 0 ??
and what is, l=System.in.read(b,0,255); if l is euqal to 0 then this would be
0=System.in.read(b,0,255) // what does b,0,255 mean ??
is it checking the size of user input ??
and what is this outsite the catch,
S=new String (b,0,l-2);
?

l is initialised to 0 so that it will have a value in the catch clause if the read throws an exception.
Check the API doc for the read method - the 3 parameters are
a byte array to read the data into
an int showing where in the array to start (usually 0)
an int showing the max number of bytes to read
It returns an int showing the number of bytes actually read, or -1 if it just hit end-of-file. This value is assigned to the stupid variable l.

S=etc creates a new string from the bytes that were read, but omitting the last 2 bytes. I have no idea why. l will probably be 0 if there is an exception, so this will give an invalid value for the String constructor.

ps: Dupron. By all means please join in and help wherever you can - but there's no point just repeating what a previous poster has already said.

Edited 6 Years Ago by JamesCherrill: n/a

what will be the value of l there, ?

S=new String (b,0,l-2);

and if the S already initialize then why new String is written there ?

If the read is successful, l contains the number of bytes read.
If the read just found end-of-file l will be -1
If the read crashes out with an exception l will be 0

S is not initialised before that line, it's declared, but not initialised.
new String(b, ...) converts an array of bytes into a new String, so this takes the byte array read in on line 6 and converts it into an ordinary String.

Suppose the input is ABC, what will be the value of l ?

Edited 6 Years Ago by Xufyan: n/a

Come on now. You have to do some work yourself here.
Add a print statement to print the value of l and feed in "ABC" as the input and see what happens.
Experiment with print statements to find out how code works.

Come on now. You have to do some work yourself here.
Add a print statement to print the value of l and feed in "ABC" as the input and see what happens.
Experiment with print statements to find out how code works.

thankshh..:)
i've printed the value of l and input abc, i get the size of l=5,

now see this,

try{
l=System.in.read(b,0,255);
}
catch (Exception r){}
S=new String (b,0,l);

0=System.in.read(b,0,255)
i input abc so l becomes 5,
5=abc,

now what this line will do S=new String (b,0,l); since we 've already get the input so what is the purpose of this line ?
is it converting that 5 into abc ??

Edited 3 Years Ago by mike_2000_17: Fixed formatting

b is an array of bytes, it has 5 elements
b[0] = 'a'
b[1] = 'b'
b[2] = 'c'
and the other two are probably a linefeed and a carriagereturn (but this is system-dependent)
The new String takes the first (5-2, ie 3) elements and turns them into a single String "abc"

This article has been dead for over six months. Start a new discussion instead.