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

perdoname_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
Try using this:
 
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) {
      System.out.println(line); // Print to stdout
      line = reader.readLine();
    }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
perdoname_Author Commented:
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(){}
}


0
Ajay-SinghCommented:
> Compressor(line);

 
What are you trying to do here?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

perdoname_Author Commented:
i want to pass the text file to the compressor program :S

0
Ajay-SinghCommented:
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
0
perdoname_Author Commented:
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:
http://www.experts-exchange.com/Programming/Languages/Java/Q_23147846.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!!!
0
Ajay-SinghCommented:
If you mean compress by encode, you should replace
 
Compressor(line);

 
with
 
new Compressor().encode(line.getBytes());
0
perdoname_Author Commented:
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(){}
}

0
Ajay-SinghCommented:
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
0
perdoname_Author Commented:
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]
0
Ajay-SinghCommented:
>  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
0
perdoname_Author Commented:
oh thanks!
something last.... can you give a look to the specifications that are in this thread: http://www.experts-exchange.com/Programming/Languages/Java/Q_23147846.html  and tell me if the above code [in this thread] is doing the required task?

Thanks in advance!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.