Results 1 to 5 of 5
- 06-23-2010, 04:47 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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.io.*; import java.util.*; class boxes { public static void main(String[] args) { String s1 = ""; String s2 = ""; String s4 = ""; int a = 0; Scanner sc = new Scanner(System.in); InputStreamReader input = new InputStreamReader(System.in); 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.exit(0); } } 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
- 06-23-2010, 05:52 AM #2
edit
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 substringLast 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
-
Recommendation 1 unrelated to your question:
Never do this:
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.Java Code:catch (Exception e){}
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
use something likeJava Code:int a; String s1; String s2; String s3: String s4;
Recommendation 4 related to your question:Java Code:int sideLength; String boxTop; String boxInnerSide; String boxSide; String boxBottom;
You know that s1 and s4 (boxTop and boxBottom) are exactly the same, right?
- 06-23-2010, 07:51 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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;
kind regards,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('*'); else sb.append(' '); sb.append('\n'); } System.out.print(sb.toString());
Jos
- 06-30-2010, 07:22 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
making a program emulate roulette HELP!!
By manowar689 in forum New To JavaReplies: 13Last Post: 06-24-2010, 02:13 AM -
Making a program out of a Java Applet
By Bomber_Will in forum Java AppletsReplies: 1Last Post: 07-16-2009, 02:54 AM -
Making the program
By pheonix in forum Advanced JavaReplies: 4Last Post: 04-18-2009, 06:36 AM -
Help Making a program not crash
By Lifeis2evil in forum New To JavaReplies: 2Last Post: 12-10-2008, 03:10 AM -
Need help with Java classes for making a program.
By TheDarkReverend in forum Advanced JavaReplies: 2Last Post: 11-28-2008, 04:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks