Results 1 to 11 of 11
  1. #1
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default standard input / output in console

    i'm talking about console, i want to ask a question (this is the most question i asked about java), how can java do this:

    Java Code:
    //this is in C++
    #include <iostream>
    using namespace std;
    
    int main () {
    	int num1, num2;
    	char op;
    
    	cout << "enter: ";
    
    	//how can java do like this?
    	cin >> num1 >> op >> num2;
    
    	//...
    	return 0;
    }
    and what's the standard I/O in java, for console program?
    Last edited by chipp; 05-10-2012 at 03:44 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: standard input / output in console

    Read all about it in the API documentation for the Scanner class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: standard input / output in console

    For the output part, use the System.out methods like print and println
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: standard input / output in console

    does it contained in java.util.Scanner? btw, can you give me some short example?

    @norm: yes, i understand about the "System.out" but what blanks me is about the input stream, and afterall, i never seen that kinda input in java (sequential input for (different types) variables)
    Last edited by chipp; 05-10-2012 at 04:19 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: standard input / output in console

    Use Search on this forum for lots of code samples.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: standard input / output in console

    i've read from:
    Scanner (Java 2 Platform SE 5.0)
    in this line:
    Java Code:
    Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
    what does "\\s*" do? and does the command is for deleting the words "fish" when the input is loads to the stream?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: standard input / output in console

    The String in the ()s is a regular expression. Whole different topic from java programming.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: standard input / output in console

    i tried to search through the forum, but there's just an repeated input assignment rather than doing all at once:

    Java Code:
    Scanner new_scan = new Scanner(System.in);
    int some_int = new_scan.nextInt();
    char some_char = new_scan.next();
    int some_int2 = new_scan.nextint();
    this using a lot of commands instead of just:
    Java Code:
    //C++
    int some_int, some_int2;
    char some_char;
    
    //...
    
    cin >> some_int >> some_char >> some_int2;
    is there's any suggestions?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: standard input / output in console

    any suggestions?
    Write it the way that it is done in the java language. There is no operator redefinition/overloading. You will have to write all the code to do it. Your fingers will get sore and tired, but there won't be the hidden code being generated that can be confusing and error prone..
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: standard input / output in console

    oh... so, i guess, this "case" is closed... :D

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: standard input / output in console

    Quote Originally Posted by chipp View Post
    Sooner or later, you're going to miss out on something if you keep referring to old API for EOLed versions. Bookmark these:
    Java Platform SE 6
    Java Platform SE 7

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Standard input
    By Jey in forum New To Java
    Replies: 2
    Last Post: 08-18-2011, 10:31 AM
  2. Redirect standard output
    By Cylab in forum New To Java
    Replies: 3
    Last Post: 08-07-2010, 04:35 PM
  3. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 08:13 PM
  4. How Do You?? Get the Console Output as a GUI??
    By Lyricid in forum AWT / Swing
    Replies: 10
    Last Post: 11-20-2009, 11:35 PM
  5. Logging Standard output to a file
    By jhyuk in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2009, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •