Hi, recently I started a course at University in programming.
The chosen language was Java which I have no experience in.
Though I do have some basic knowledge of C++.
One of the most basic programs we are asked to write is the standard
Hello world!
This is quite easily accomplished in both languages.
I decided I would try and extend myself into not only posting to the I/O stream but reading from it.
Though this seems remarkably more difficult in Java then it is in C++. With some googling however I came up with the following code.
import java.io.*; public class GetUserInput { public static void main (String[] args) { System.out.print("Enter your name and press Enter: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String name = null; try { name = br.readLine(); } catch (IOException e) { System.out.println("Error!"); System.exit(1); } System.out.println("Hello, " + name); } }
Now to my problem.
One of the programs which I have learned to write in C++ is a program which reads the stream and wraps what is written in Asterisk , is pulls the size of the input and then adds as many asterisk as needed to surround the word in a frame.
// ask for a person's name, and generate a framed greeting #include <iostream> #include <string> int main() { std::cout << "Please enter your first name: "; std::string name; std::cin >> name; // build the message that we intend to write const std::string greeting = "Hello, " + name + "!"; // build the second and fourth lines of the output const std::string spaces(greeting.size(), ' '); const std::string second = "* " + spaces + " *"; // build the first and fifth lines of the output const std::string first(second.size(), '*'); // write it all std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; return 0; }
The program would then run like.
So, is there anyway easier way to mirror the function of std::cout and std::cin in java?
And what function is needed to create new strings then get the size of the string and rather than outputting it as an int. Using that size to post asterisks in a box around the outputted text.
Thanks for any help you can give.