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.


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

Results 1 to 3 of 3

Thread: Hello, "name" problem.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Default Hello, "name" problem.

    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;
    			name = br.readLine();
    		catch (IOException e) 
    		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.

  2. #2
    Junior Member
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Default Re: Hello, "name" problem.

    I can't answer your question directly, but if i was writing that code I would simply use .length() to get the string's length and then calculate the frame from there. You may also want to take a look at StringBuilder.

    It makes it a bit easier to work with strings as you can append, insert etc.. For instance you can do this to draw a line of asterisks:

    StringBuilder sb = new StringBuilder(30); 
    for (int i = 1; i <= 30; i++) { 

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    North Wales
    My Mood
    Thanked 115 Times in 106 Posts
    Blog Entries

    Default Re: Hello, "name" problem.

        public String starWrap(String regex) {
            String searching = "* Searching for " + regex + " *";
            String star = "";
            for (int t = 0; t < searching.length(); t++) {
                star = star + "*";
            return star + "\n" + searching + "\n" + star;

    Here's a method I used for quick formatting of text in one of my last assignments.
    Just replace "Searching for" to whatever you want.
    Dumping code just because it's a pretty darn basic piece of code and no point in wasting anybody's time.
    Using it in such a method means its reusable for what you need.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. problem using the "extend"
    By Eildotcom in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 06:54 PM
  2. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  3. "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, 08:35 AM
  4. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM