Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How do I include an "iostream" parallel?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I include an "iostream" parallel?

    Made some last minute "business" decisions. I am no longer going to try programming in Java. It's a dead end for my ambitions. ... so many changes us youth go through -_-'

    This is my basic C++ program. I'm trying to clone it in java.
    #include <iostream>
    using namespace std;
     
    int main ()
    {
    	int i;
    	cout << "Your story begins NOW!!!";
    	cout << endl;
    	cout << "Please enter your age: ";
    	cin >> i;
    	cout << endl;
    	cout << "The value you entered is: " << i;
    	cout << endl;
    	system("PAUSE");
    	return 0;
    }

    For java so far all I have is
    import java.io.*;
    import java.util.*;
     
    public class initiate {
    		public static void main(String[] args) {
    			System.out.println("Your story begins NOW!!!\n");
    			System.out.println("Enter your age: \n");
    			Scanner scan = Scanner(System.in);
    		}
    }

    What am I missing? I receive this error

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The method Scanner(InputStream) is undefined for the type initiate
     
    	at initiate.main(initiate.java:9)
    Last edited by keysle; May 29th, 2012 at 02:21 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,633
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: How do I include an "iostream" parallel?

    You have coded a method call, not a call to a constructor. Add new.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do I include an "iostream" parallel?

    int i;
    cout << "Your story begins NOW!!!";
    cout << endl;
    cout << "Please enter your age: ";
    cin >> i;
    cout << endl;
    cout << "The value you entered is: " << i;
    cout << endl;
    system("PAUSE");
    return 0;
    }

    Replace cout << text with System.out.println(text)

    Replace cin >> with

    scan.nextInt() for integer
    scan.nextDouble() for double
    scan.nextLine() (for a whole line) or scan.next() (for the next String that comes before you reach a space.) for String
    scan.nextLine() (or scan.next()) anyway scan.nextLine().charAt(0) for a char readin. (There is no nextChar() or nextCharacter() method.)

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: How do I include an "iostream" parallel?

    Corrected your issue:
    Scanner scan = new Scanner(System.in);

    When instantiating an object in java you have to invoke it using the keyword "new"
    Last edited by Parranoia; May 31st, 2012 at 12:04 AM.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do I include an "iostream" parallel?

    That's what Norm already said.

    Quote Originally Posted by Norm
    You have coded a method call, not a call to a constructor. Add new.
    As for

    system("Pause"), that's a system constructor or a method called system. I can surmise that much. Whether a constructor or a method, it's being passing the cstring "Pause".
    If it's a sort of wait, like you want to wait for a second or so, then try

    Thread.sleep(long millis);

    Anyway, I found this about system("Pause")

    System("PAUSE"); why is it wrong to use? - C++ Forum

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I include an "iostream" parallel?

    Sorry for the late response.
    blasts. I thought I managed to get this.

    Java penguin. Isn't system() only used for debugging anyway? I d on't see why the console would be used in a public product.

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM