Results 1 to 5 of 5
  1. #1
    cid is offline Member
    Join Date
    Jun 2010
    Rep Power

    Default Need help making program more efficient

    Alright well I have a version of this program which creates a box for inputs >2 The width is supposed to be the integer input, and so is the height. Please tell me if there are any unneccessary lines which I can edit/remove to make the program more concise. Also, the space between the top line and bottom line cannot be asterisks. It must be spaces. Specifically my professor said we needed only three "for loops" but I have four. How can I lose one and keep the program working correctly? We are in the beginning of the class and the furthest we have gotten is for loops.

    Java Code:
    import java.util.*;
    class boxes {
    public static void main(String[] args) {
    String s1 = "";
    String s2 = "";
    String s4 = "";
    int a = 0;
    Scanner sc = new Scanner(;
    InputStreamReader input = new InputStreamReader(;
    BufferedReader reader = new BufferedReader(input);
    try {
    System.out.println ("Enter a number.");
    //start of first line
    a = sc.nextInt();
    for (int x=0; x<a; x++){
    s1 = (s1 + '*');
    if (a<=2){
    System.out.println ("Invalid entry");
    System.out.println (s1);
    //end of first line
    //start of middle portion
    for (int x=0; x<a-2; x++){
    s2 = (s2 + ' ');
    String s3 = ('*'+s2+'*');
    for (int x=0;x<a-2;x++){
    System.out.println (s3);
    //end of middle portion
    //start of last line
    for (int x=0; x<a; x++){
    s4 = (s4 + '*');
    System.out.println (s4);
    //end of last line
    //end of program
    catch (Exception e){}
    Last edited by Fubarable; 06-23-2010 at 05:55 AM. Reason: code tags added

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    UK. near London
    Rep Power



    encapsulate the a = sc.nextInt(); in a do while loop to check if they entering a number larger than 2

    Firstly only declare 2 string variable, one for the top row and one for the middle row, you dont need one for the third because its gonna contain the same data has the first. name them something like String row1; String gap.

    then do a do-while loop on the part where you ask to enter a number and then you assign the number. the do while loop will ensure the number is bigger than 2 and if it is not then it will keep repeating the question. Also put a try-catch statment inside the do part of the loop where you write sc.nextInt; and in the catch part write sc.nextLine(), you do the sc.nextLine() otherwise when the code repeats the do-loop it will just keep asking the question in and endless loop.

    then after that do one for-statment and thats where you put the asterix in the row1 variable and a space in the gap variable. so you'll end up with like 5 asterixs and 5 spaces.

    then you simple System print out the row1 variable
    then print out the gap variable in another for loop so that it prints a certain amount of times to make the box the right size
    then print out the row1 variable again

    you will find that the gap variable will be too large by 2 spaces, so you need to use substring method on it as your printing it off. so research substring
    Last edited by alacn; 06-23-2010 at 09:26 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Recommendation 1 unrelated to your question:
    Never do this:
    Java Code:
    catch (Exception e){}
    except in very specific circumstances when you know what you're doing. Else, if your program throws an exception, you'll never know what caused the exception and why.

    Recommendation 2 unrelated to your question:
    Please use code tags and decent formatting when posting to this forum. Doing this will make your code much easier to read and will make it much easier for others to help you, and this will likely get you faster and better help. I've taken the liberty of adding code tags to your code, but this unfortunately doesn't help make it more readable as your indentation and code formatting, to be blunt, is terrible.

    Recommendation 3 unrelated to your question:
    Try to use variable names that make sense, that are "auto-commenting". In other words, instead of
    Java Code:
    int a;
    String s1;
    String s2;
    String s3:
    String s4;
    use something like
    Java Code:
    int sideLength;
    String boxTop;
    String boxInnerSide;
    String boxSide;
    String boxBottom;
    Recommendation 4 related to your question:
    You know that s1 and s4 (boxTop and boxBottom) are exactly the same, right?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    Never use Strings to 'build' them; that is awfully expensive; it doesn't matter much for a box of size, say, 5x6 but for long Strings your method is killing performance and memory. Use a StringBuilder (or StringBuffer) instead; I'd do it like this;
    Java Code:
    int N, M; // size of the box >= 3
    StringBuilder sb= new StringBuilder();
    for (int i= 1; i <= N; i++) {
       for (int j= 1; j <= M; j++)
          if ((i == 1) || (i == N) || (j == 1) || (j == M)) // on the side of the box?
             sb.append(' ');
    kind regards,


  5. #5
    cid is offline Member
    Join Date
    Jun 2010
    Rep Power


    thanks for the help all and i will try to make my code easier to understand in the future

Similar Threads

  1. making a program emulate roulette HELP!!
    By manowar689 in forum New To Java
    Replies: 13
    Last Post: 06-24-2010, 02:13 AM
  2. Making a program out of a Java Applet
    By Bomber_Will in forum Java Applets
    Replies: 1
    Last Post: 07-16-2009, 02:54 AM
  3. Making the program
    By pheonix in forum Advanced Java
    Replies: 4
    Last Post: 04-18-2009, 06:36 AM
  4. Help Making a program not crash
    By Lifeis2evil in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 03:10 AM
  5. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts