Link to home
Start Free TrialLog in
Avatar of perdoname_
perdoname_

asked on

stdin/stdout

Hello,
Can anyone help me to add a main class to the code in the code snippet which will take the input with stdin and return it with stdout ??

Thanks in advance!
import java.lang.String;
import java.text.ParseException;
 
public abstract class Compressor
{
 
    static final String		REV =
 
    public static final int	SERIES =	100;
 
 
    public static methods
 
    public static final char toHex(int v)
    {
        // Convert a 4-bit value into a hex digit char
        if (v >= 0x0  &&  v <= 0xF)
            return ((char)(v < 0xA ? v+'0' : v-0xA+'A'));
        return ('?');
    }
 
 
    public static final int fromHex(char ch)
    {
        if (ch <= '9')
        {
            if (ch >= '0')
                return (ch-'0');
            return (-1);
        }
 
        if (ch <= 'F')
        {
            if (ch >= 'A')
                return (ch-'A'+0xA);
            return (-1);
        }
 
        if (ch <= 'f')
        {
            if (ch >= 'a')
                return (ch-'a'+0xA);
            return (-1);
        }
 
        return (-1);
    }
 
 
    public static String encode(/*const*/ byte[] data)
    {
        return (encode(data, 0, data.length));
    }
 
    public static String encode(/*const*/ byte[] data, int off, int len)
    {
        char[]	ch;
        int	i;
 
        ch = new char[data.length*2];
        i = 0;
 
        while (len-- > 0)
        {
            int		b;
            int		d;
 
            b = data[off++] & 0xFF;
 
            d = b >> 4;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;
 
            d = b & 0xF;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;
        }
 
        return (new String(ch));
    }
 
    private Compressor()
    {}
    
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of perdoname_
perdoname_

ASKER

Thanks for the code !!
Can you give a look if its right the way i did place the void main in the code??

Thanks in advance!


import java.lang.String;
import java.util.*;
import java.io.*;
import java.text.ParseException;

public abstract class Compressor
{

    static final String      REV = "";

    public static final int SERIES =      100;


    public static final char toHex(int v)
    {
        if (v >= 0x0  &&  v <= 0xF)
            return ((char)(v < 0xA ? v+'0' : v-0xA+'A'));
        return ('?');
    }


    public static final int fromHex(char ch)
    {
        if (ch <= '9')
        {
            if (ch >= '0')
                return (ch-'0');
            return (-1);
        }

        if (ch <= 'F')
        {
            if (ch >= 'A')
                return (ch-'A'+0xA);
            return (-1);
        }

        if (ch <= 'f')
        {
            if (ch >= 'a')
                return (ch-'a'+0xA);
            return (-1);
        }

        return (-1);
    }


    public static String encode(/*const*/ byte[] data)
    {
        // Encode the binary data as radix-16 characters
        return (encode(data, 0, data.length));
    }


    public static String encode(/*const*/ byte[] data, int off, int len)
    {
        char[]      ch;
        int      i;

        // Convert bytes to hex digits
        ch = new char[data.length*2];
        i = 0;

        while (len-- > 0)
        {
            int            b;
            int            d;

            // Convert next byte into a hex digit pair
            b = data[off++] & 0xFF;

            d = b >> 4;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;

            d = b & 0xF;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;
        }

        return (new String(ch));
    }



    public static void main(String[] args) throws IOException
    {
   BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
   String line = reader.readLine(); // Read from stdin
   while (line != null && line.length() > 0) {
       Compressor(line);
       System.out.println(line); // Print to stdout
       line = reader.readLine();
    }

    }


    public Compressor(){}
}


> Compressor(line);

 
What are you trying to do here?
i want to pass the text file to the compressor program :S

Couple of suggestions:
 
1. The class Compressor should not be abstract.
2. I don't find compress method, there are encode/decode methods. You should
have compress method that takes String
actually thats what i want to do to encode text files. i though that this code can be used as an answer to one of previous questions i did in the forum. Can you give a look at the below site:
https://www.experts-exchange.com/questions/23147846/Pointer-to-500-points-q-About-an-encoder.html ... its the specs of a task that i have to accomplish and if you can/want tell me if that code can be use for that purpose

Thanks in advance!!!
If you mean compress by encode, you should replace
 
Compressor(line);

 
with
 
new Compressor().encode(line.getBytes());
I did replace it but when im running the compressor from the terminal it does not return anything :S
[java Compressor ../1.txt] and it returns a single blank line
Do you know why's that happening ?

import java.lang.String;
import java.util.*;
import java.io.*;
import java.text.ParseException;

public class Compressor
{

    static final String      REV = "";

    public static final int SERIES =      100;


    public static final char toHex(int v)
    {
        if (v >= 0x0  &&  v <= 0xF)
            return ((char)(v < 0xA ? v+'0' : v-0xA+'A'));
        return ('?');
    }


    public static final int fromHex(char ch)
    {
        if (ch <= '9')
        {
            if (ch >= '0')
                return (ch-'0');
            return (-1);
        }

        if (ch <= 'F')
        {
            if (ch >= 'A')
                return (ch-'A'+0xA);
            return (-1);
        }

        if (ch <= 'f')
        {
            if (ch >= 'a')
                return (ch-'a'+0xA);
            return (-1);
        }

        return (-1);
    }


    public static String encode(byte[] data)
    {
        return (encode(data, 0, data.length));
    }


    public static String encode(byte[] data, int off, int len)
    {
        char[]      ch;
        int      i;

        ch = new char[data.length*2];
        i = 0;

        while (len-- > 0)
        {
            int            b;
            int            d;

            b = data[off++] & 0xFF;

            d = b >> 4;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;

            d = b & 0xF;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;
        }

        return (new String(ch));
    }



    public static void main(String[] args) throws IOException
    {
   BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
   String line = reader.readLine(); // Read from stdin
   while (line != null && line.length() > 0) {
       new Compressor().encode(line.getBytes());
       System.out.println(line); // Print to stdout
       line = reader.readLine();
    }

    }

    public Compressor(){}
}

This code read input from stdin, which was your original question. So,
passing file parameter will have not effect. You should type for the input
on the console instead.
 
If you want to read from file, try this example
 
http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html
no i still want to use it as a stdin cuz actually the code is going to be tested through a script.
Because i dont have the script can you tell me if its going to work ? i mean is there anything else missing or do i have to add something more to work [?]
Thanks!
[sorry for asking you so many things]
>  i mean is there anything else missing
You are confused. You said, you want to read from stdin and pass a file as
input parameters to the program.
 
> do i have to add something more to work
No. Just type the lines you want to encode. or use
 
java Compressor < ../1.txt
oh thanks!
something last.... can you give a look to the specifications that are in this thread: https://www.experts-exchange.com/questions/23147846/Pointer-to-500-points-q-About-an-encoder.html  and tell me if the above code [in this thread] is doing the required task?

Thanks in advance!