Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Returning a output statement
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
S_Grimm




PostPosted: Wed Oct 22, 2008 10:50 am   Post subject: Returning a output statement

I just finished writing the code to read a pile of stuff without declaring the BufferedReaderInput stuff in the front. what i want to is how to return a output statement.
Java:

import java.io.*;
public class read
{
    public static void main (String [] args) throws IOException
    {
        read in = new read (); //creates the statement that allows me to read using defined statements
    }


    public static int readInt () throws java.io.IOException //the reader for Integers
    {
        InputStreamReader input = new InputStreamReader (System.in);
        BufferedReader reader = new BufferedReader (input);
        int Int; //declares the variable
        Int = Integer.parseInt (reader.readLine ()); //tells the program to read the input and convert it to int
        return Int; //returns Int to main program
    }


    public static String readString () throws java.io.IOException // the reader for strings
    {
        BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
        String string;
        string = input.readLine ();
        return string;
    }


    public static double readDouble () throws java.io.IOException
    {
        InputStreamReader input = new InputStreamReader (System.in);
        BufferedReader reader = new BufferedReader (input);
        double Dub;
        Dub = Double.parseDouble (reader.readLine ());
        return Dub;

    }


    public static float readFloat () throws java.io.IOException
    {
        InputStreamReader input = new InputStreamReader (System.in);
        BufferedReader reader = new BufferedReader (input);
        float flo;
        flo = Float.parseFloat (reader.readLine ());
        return flo;

    }
}


Theres the code for input. i want to write an OUTPUT version of this...
Any help appriciated.
A\V
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Wed Oct 22, 2008 3:09 pm   Post subject: RE:Returning a output statement

Just put System.out.print() or println() calls before the input in each function.

If you're using Java 1.5 you don't have to make this library, since there is already a Scanner class that does input much nicer than BufferedReader and co.
rdrake




PostPosted: Wed Oct 22, 2008 10:20 pm   Post subject: RE:Returning a output statement

If you're using Java 1.6 there's a new Console class that does things very similarly to .NET's Console class.

Note: If using Eclipse or possibly other IDEs, the System.Console() object will be null. I don't know why this is, it just is. You have to run from a regular console for the object to be of any use.

If you want it to work in Eclipse's little built-in console or you need to target Java 1.5, use [Gandalf]'s suggestion from above.
[Gandalf]




PostPosted: Thu Oct 23, 2008 7:05 am   Post subject: Re: RE:Returning a output statement

Gah, Java 1.6 stuff is already making 1.5 almost obsolete, I have to get with the times. Sad Anyone know a brief summary of new syntax, classes or other important changes in 1.6? I know there's an official list of changes, but that's far too lengthy to skim.

rdrake wrote:
Note: If using Eclipse or possibly other IDEs, the System.Console() object will be null. I don't know why this is, it just is. You have to run from a regular console for the object to be of any use.

I'm guessing, from the link you posted:
java.sun.com wrote:
If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
S_Grimm




PostPosted: Thu Oct 23, 2008 7:38 am   Post subject: Re: Returning a output statement

technically i am using Holtsofts Read to Program Java IDE, but I refuse to use the hsa.Console class built in. i can alos use the most recent version of JDK, which is 1.6 i think.... and i want to be able to write my own library for knowledge.

Anyway. i tried what you said gandalf, and i can't seem to get it to work.......
Java:

import java.io.*;
public class write
{
    public static void main (String[] args) throws IOException
    {
        write in = new write (); //creates the statement that allows me to write
        }
       
    public static String writeString () throws java.io.IOException
    {
        BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
        String string;
        string = System.out.println (input.readLine ());
        return string;
    }


}

There is the code, if someone can pont out the flaw...
[Gandalf]




PostPosted: Thu Oct 23, 2008 7:50 am   Post subject: Re: Returning a output statement

Since your original post was unclear, I was assuming you wanted to prompt the user before input, like so:
Java:
public class InTest {
        public static void main(String[] args) {
                int age = getNum("Enter your age:");
                System.out.println("You are " + age + " years old.");
        }
        public static int getNum(String prompt) {
                System.out.print(prompt);
                //...input code goes here
                //...return num
        }
}

However, if you're looking to write to a file (Is this what you're asking?) for example, Google it. The process will be comparable to input, however you definitely won't be using Reader objects!
S_Grimm




PostPosted: Thu Oct 23, 2008 8:33 am   Post subject: RE:Returning a output statement

i want to write a library that i can use to output a statement by calling it instead of using System.out.println everytime.
[Gandalf]




PostPosted: Thu Oct 23, 2008 8:55 am   Post subject: RE:Returning a output statement

First, are you doing this for school, practice, or to actually use?

If your idea is to make Java more convenient, you're going about it absolutely the wrong way. For one, you should be using existing Java libraries, such as Scanner or Console. The console that rdrake referred to isn't the HSA Console class, but one that exists in the official 1.6 distribution. They're two completely different entities. These things all exist, you just have to learn how to use them, and better yet how to read the Java documentation.

You don't have to make a new method for output, you can simply:
code:
import java.io.*;
...
PrintStream out = System.out;

or
code:
import static java.lang.System.out;

or any other multitude of ways to shorten the call to print/ln/f. However, you should first understand why this is generally bad practice, and to do that you should first learn more of the language.

You can't just skip over some concepts and expect to jump right into creating programs or libraries or whatever. You have to go step by step.
Sponsor
Sponsor
Sponsor
sponsor
S_Grimm




PostPosted: Thu Oct 23, 2008 10:33 am   Post subject: Re: Returning a output statement

this is for me, when i try to write a large program and want to be able to "simplify" the code. and i know that the reference wasn't to HSA Console. HSA Console is just what come with Ready to Program. The Textbook we are Assigned uses HSA Console. The teacher doesn't want us to use HSA, he wants us to use System.out.println. I just want to be able to shorten it to something like
code:
put.text("Hello");

where put is calling the method "text"
code:
write put = new write();
from the file write.java.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: