2

It is homework question.

Question:

"Use an array of integers with size 10 and print the content of the array using write( ) method.

Note: Please do not use System.out.println( ) method."

This is what I have tried so far, but it doesn't work.

int i = 0; while (i != array.length) { byte[] temp = new byte[4]; temp[0] = (byte) (array[i] & 256); array[i] = array[i] >> 8; temp[1] = (byte) (array[i] & 256); array[i] = array[i] >> 8; temp[2] = (byte) (array[i] & 256); array[i] = array[i] >> 8; temp[3] = (byte) (array[i] & 256); System.out.write(temp, 0, 4); i++; } 

I can't find a way to do this, please help me out.

Thanks in advance. :)

2
  • 4
    Am I the only one that thinks this is WAY overcomplicated given the problem statement? Commented Nov 10, 2010 at 13:26
  • What was wrong with revision 2? Commented Nov 11, 2010 at 13:13

4 Answers 4

3
void printInt(int x) { String s = String.valueOf(x); for(int i = 0; i < s.length(); i++) { System.out.write(s.charAt(i)); } System.out.write('\n'); } 

Edit: prettier:

void printInt(int x) { String s = String.valueOf(x); System.out.write(s.getBytes()); System.out.write('\n') } 
Sign up to request clarification or add additional context in comments.

2 Comments

The edited one must be like this:- System.out.write(s.getBytes(), 0, s.getBytes().length);
System.out inherits public void write(byte b[]) from FilterOutputStream, which simply calls the function you mentioned, so it's just semantics.
0

Why are you making an array of bytes ? There is a write(int b) method.

Integer array = new Integer[10]; // populate array with some integers for (Integer i : array) { System.out.write((int)i); } 

even the cast (int)i isnt necessary as autoboxing will take care of that.

2 Comments

Because System.out is a PrintStream any call to System.out.write(int) will convert the int value to an ASCII char.
Even though this solution seems to have moved up, but this is essentially wrong
0

The trick is to convert each digit of the int into a char. and then write the char using System.out.write.
So for example, if one of the int values is 91. You'll need to do
System.out.write('9');
System.out.write('1');
System.out.flush();

Comments

0

Have an integer array of size 10, i.e. int[] i = new int[10] and write each of them in System.out.write(int b) where the write() method accept an integer as parameter.


This is really ugly but it worked....

/** * */ package testcases; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * @author The Elite Gentleman * */ public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] array = {10, 20, 30, 40, 50}; StringBuffer sb = new StringBuffer(); for (int i: array) { sb.append(String.valueOf(i)); } try { System.out.write(sb.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

2 Comments

This isn't working:- int i = 0; while (i != array.length) { System.out.write(array[i]); i++; }
that won't work! the 'write' method accepting the 'int' argument treats the int as the byte representation. so it's highly unlikely that you'll see any of the int values getting printed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.