?
Solved

stdin/stdout

Posted on 2008-02-10
12
Medium Priority
?
3,365 Views
Last Modified: 2008-03-06
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

0
Comment
Question by:perdoname_
  • 6
  • 6
12 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 1500 total points
ID: 20863269
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
 

Author Comment

by:perdoname_
ID: 20863444
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20863569
> Compressor(line);

 
What are you trying to do here?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:perdoname_
ID: 20863643
i want to pass the text file to the compressor program :S

0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20863703
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
 

Author Comment

by:perdoname_
ID: 20863802
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20863960
If you mean compress by encode, you should replace
 
Compressor(line);

 
with
 
new Compressor().encode(line.getBytes());
0
 

Author Comment

by:perdoname_
ID: 20863986
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20864026
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
 

Author Comment

by:perdoname_
ID: 20864074
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20864106
>  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
 

Author Comment

by:perdoname_
ID: 20864154
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question