Link to home
Start Free TrialLog in
Avatar of dleslie
dleslie

asked on

C++ IO Streams in Java?

I'm a long time programmer in C++, and have gotten use to it's use of IO streams. My question is, what's the simplest way to recreate the following code in Java?

#include <iostream>

void main()
{
 char string1[100], string2[100]
 cin >> string1 >> string2;
 cout << string1 << string2 << endl;
}

Thanks...
Avatar of dleslie
dleslie

ASKER

Incase you didn't read into my question, I'm looking for FULL source :)
Hello dleslie,

Try to review the following code:


import java.io.*;

public class IODemo {
   public static void main(String [] args) {
        String string1=null, string2=null;
        try {
             DataInputStream dis = new DataInputStream(System.in);
             string1 = dis.readLine();
             string2 = dis.readLine();
       } catch (IOException ioe) {
             System.out.println("IOException: "+ ioe.toString());
             System.exit(1);
       }
       System.out.println(string1 + string2);
  }
}


Good luck,
--
Avatar of dleslie

ASKER

Close, but not exactly what I'm looking for.

The way you designed requires two carriage returns, the c++ code I showed required one. Eg,
User enters:
Hi There

Hi is stored in string1
There is stored in string2

Output:
Hi There


BUT, your java code would require the user to enter:
Hi
There

You see what I mean? This is a problem I've been trying to get around for a while...

Thanks,
-Dan
Avatar of dleslie

ASKER

BTW, I was replying to the comment, that wasn't quite the answer I was looking for :)

-Dan
You could input into one string and use the StringTokenizer class to separate the tokens into different string variables...
ASKER CERTIFIED SOLUTION
Avatar of jjdunne
jjdunne

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I believe that's what I had in mind ... except you may have to specify what char(s) you want as delimiters...
Avatar of dleslie

ASKER

Hmm, I'll try that, but right now, I've got Corel Linux to install, so, I'll post again in a few (hours)?

Thanks,
-Dan
Avatar of dleslie

ASKER

Works perfectly! So, who's going to propose it as an answer? I'm inclined to give it to jjdunne (for actually proposing code)...

-Dan
Avatar of dleslie

ASKER

Exactly!
go for it, jjdunne...