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...
#include <iostream>
void main()
{
char string1[100], string2[100]
cin >> string1 >> string2;
cout << string1 << string2 << endl;
}
Thanks...
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("IOExce ption: "+ ioe.toString());
System.exit(1);
}
System.out.println(string1 + string2);
}
}
Good luck,
--
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("IOExce
System.exit(1);
}
System.out.println(string1
}
}
Good luck,
--
To understand IO streams in Java visit foll.website...
http://developer.java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/index.html
http://developer.java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/index.html
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
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
ASKER
BTW, I was replying to the comment, that wasn't quite the answer I was looking for :)
-Dan
-Dan
You could input into one string and use the StringTokenizer class to separate the tokens into different string variables...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe that's what I had in mind ... except you may have to specify what char(s) you want as delimiters...
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
Thanks,
-Dan
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
-Dan
ASKER
Exactly!
go for it, jjdunne...
ASKER