0

Hi I was wondering if somebody could help me i am completelty stuck
I want to read in triangle measurements from the command line check if the tri is square then when the user is done print out the amount of squares.

the user would signfy that s/he was done by instead of entering another set of values would just hit return again. But putting the "Number of square.." line up one notch as to where the last return would place it.

so it would look like this

Enter a series of length-height pairs
2 3
4 4
6 6
3 4
Number of squares: 2

here is my attempt i am using a predefined class console, but it doesn't necessarily have to be used (but would be good if it was).. I have tried every thing but cant get it. Please help.

import java.io.*;

class RecTangle 
{
	int length;
	int height;

	void get()
	{
		length = Console.readInt();
		height = Console.readInt();
	}

	boolean isSquare()
	{
		return(length == height);
	}

}

class IsRecSquare
{
	static int count;
	public static void main(String [] args)
	{
	
		do
		{

			RecTangle r = new RecTangle();
			r.get();
			if (r.isSquare())
			{
				count ++;
			}

		}while (Console.peekChar() = '\r');
	//	}catch(IOException e){}
		System.out.println("The number of squares are : " + count);
	}
};





======== and  class Console




import java.io.*;

public class Console {

    private static BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in)); // the input stream
    private static String buffer = "";
    private static int p = 1; // buffer[p..] contains next input

    private static String getToken() throws IOException { // return next token from input stream
        while (buffer != null && (p>= buffer.length() || 
                Character.isWhitespace(buffer.charAt(p)))) {
            if (p>= buffer.length()) {
                buffer = br.readLine();
                p = 0;
            }
            else p++;
        }
        if (buffer == null) throw new IOException("Console: Unexpected end of file");
        int t = p;
        p++;
        while(p < buffer.length() &&
                !(Character.isWhitespace(buffer.charAt(p))))
            p++;
        p++;
        return(buffer.substring(t,p-1));
    }

    public static int readInt() {
    // Consume and return an integer. Trailing delimiter consumed.
        try {   
        	return Integer.parseInt(getToken());
        } 
        catch (Exception e) {
            System.err.println("Console: IO Exception in readInt");
            return 0;
        }
    }   

    public static boolean readBoolean() {
    // Consume and return a boolean. Trailing delimiter consumed.
    // Any string other than "true" (case ignored) is treated as false.
        try {   
        	return new Boolean(getToken()).booleanValue();
        } 
        catch (Exception e) {
            System.err.println("Console: IO Exception in readBoolean");
            return false;
        }
    }

    public static double readDouble() {
    // Consume and return a double. Trailing delimiter consumed.
        try {
        	return new Double(getToken()).doubleValue();
        } 
        catch (Exception ioe) {
            System.err.println("Console: IO Exception in readDouble");
            return 0.0;
        }
    }

    public static String readToken() {
    // Consume and return a token. Trailing delimiter consumed.
    // A token is a maximal sequence of non-whitespace characters.
    // null returned on end of file
        try {
            while (buffer != null && (p>= buffer.length() || 
                Character.isWhitespace(buffer.charAt(p)))) {
                if (p>= buffer.length()) {
                    buffer = br.readLine();
                    p = 0;
                }
                else p++;
            }
            if (buffer == null) return null;
            int t = p;
            p++;
            while(p < buffer.length() &&
                    !(Character.isWhitespace(buffer.charAt(p))))
                p++;
            p++;
            return(buffer.substring(t,p-1));
        } 
        catch (IOException ioe) {
            System.err.println("Console: IO Exception in readToken");
            return "";
        } 
    }
    
    public static char readChar() {
    //Consume and return a character (which may be an end-of-line).
        try { 
            if (buffer != null && p>buffer.length()) {
                buffer = br.readLine();
                p = 0;
            }
            if (buffer == null) throw new IOException("Console: Unexpected end of file in readChar"); 
            if (p == buffer.length()) { // supply end-of-line
                p++; 
                return('\n'); 
            }       
            else {
                p++;
                return buffer.charAt(p-1); 
            } 
        } 
        catch (IOException ioe) {
                System.err.println("Console: IO Exception in readChar");
                return (char)0;
        }
    }

    public static char peekChar() {
    // The next available character if any (which may be an end-of-line). The
    // character is not consumed. If buffer is empty return null character.
        if (buffer == null || p>buffer.length()) return('\000'); 
        else if (p == buffer.length()) return('\n'); 
        else return buffer.charAt(p); 
    }

    public static String readString() {
    // Consume and return the remainder of current line (end-of-line discarded).
    // null returned on end of file
        try {
            if (buffer!= null && p>buffer.length()) {
                buffer = br.readLine();
                p = 0;
            }   
            if (buffer == null) return null; 
            int t = p;  p = buffer.length() + 1;
            return buffer.substring(t);
       } 
       catch (IOException ioe) {
            System.err.println("Console: IO Exception in readString");
            return "";
       } 
    }

    public static int available() {
    // Number of characters available on this line (including end-of-line, 
    // which counts as one character, i.e. '\n')
        if (buffer == null) return 0;
        else return (buffer.length()+1-p);
    }
    
    public static boolean hasMoreTokens() {
    // Are there more tokens on the current line?
        if (buffer == null) return false;
        int q = p; 
        while (q<buffer.length() && Character.isWhitespace(buffer.charAt(q))) q++;
        return (q<buffer.length());
    }
    
    public static void skipLine() {
    // Skip any remaining input on this line.
        if (buffer != null) p = buffer.length() + 1;
    }

    public static void skipWhitespace() {
    // Consumes input until a non-whitespace character is entered (which
    // is not consumed).
        try {
            while (buffer != null && (p>= buffer.length() || 
                    Character.isWhitespace(buffer.charAt(p)))) {
                if (p>= buffer.length()) {
                        buffer = br.readLine(); 
                    p = 0;
                }
                else p++;
            }
        } 
        catch (IOException ioe) {
            System.err.println("Console: IO Exception in skipWhitespace");
        }
    }   
    
    public static boolean EndOfFile() { // More characters? 
        // This method is intended for use when keyboard is redirected to file
        if (available()>0) return false;
        try { 
        	buffer = br.readLine(); 
        } 
        catch (IOException ioe) {
            System.err.println("Console: IO Exception in EndOfFile");
        }
        p = 0;
        return (buffer == null);
    }        

    public static boolean endOfFile() { // More characters? 
    // alternative spelling for EndOfFile()
        // This method is intended for use when keyboard is redirected to file
        if (available()>0) return false;
        try { 
        	buffer = br.readLine(); 
        } 
        catch (IOException ioe) {
            System.err.println("Console: IO Exception in EndOfFile");
        }
        p = 0;
        return (buffer == null);
    }        

}
2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by javaAddict
0

Where are you having problems.
For reading command line I use something like this:

BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter set of values:")
String input = keyboard.readLine();

If you enter:
3 4
Then the String input will have this value: "3 4"
So you can do something like this:

import java.io.*;

class IsRecSquare
{
	static int count;
	public static void main(String [] args)
	{
	
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

		do
		{

			System.out.println("Enter set of values:")
                        String input = keyboard.readLine().trim();
                        if (input.length != 0) {
                        //code for parsing input into 2 int numbers length and height
//use: StringTokenizer or String.split(), or (String.indexOf with String.subString())
                                 if (length == height) {
                                       count++;
                                  }
                        } 

		} while (!input.equals(""));
	
		System.out.println("The number of squares are : " + count);
	}
}

Of course you will add the necessary try-catch block

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.