This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Assignment Java-1b (Resolved) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assignment Java-1b (Resolved)" Watch "Assignment Java-1b (Resolved)" New topic
Author

Assignment Java-1b (Resolved)

Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Well this one had beaten me been trying for days to get it to work right, removed the args from the command line to simplfy it until it works.

Where have I gone wrong please?

You will probbly have to compile it to see what I mean the test is below

Javaranch-Java-1b


[ April 02, 2008: Message edited by: Stuart Smith ]

Dale Carnegie:<br />"Most of the important things in the world have been accomplished<br />by people who have kept on trying when there seemed to be no hope at all."
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Looking at the picture here, it looks like you are trying to make it more difficult than it should be. Why are you counting the number of names that can fit in one screen ? Appending names one after another should be enough.


[My Blog]
All roads lead to JavaRanch
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Originally posted by Christophe Verre:
Looking at the picture here, it looks like you are trying to make it more difficult than it should be. Why are you counting the number of names that can fit in one screen ? Appending names one after another should be enough.


I am counting the names length becuase I am doing the second part where you are required to modify Java-1a so words are never split up. Assume that the console is 80 characters wide.

Scroll down its the 1b I am doing not the 1a

Also its not printing out 100 times it is much much more.
[ March 31, 2008: Message edited by: Stuart Smith ]
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Ask yourself:

How many times will the outer loop execute?
How many times will the inner loop execute for each outer loop iteration?
nameCount = consoleWidth/nameLength forgets what part of the output?
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Mate just check the length of the "some text" you are going to pass to the program via command prompt.

int len = args[0].length();


int m =80/len, test=1;

for(i=0;i<100;i++)
{
System.out.print(+args[0]+" ");

if(test++==m)
{ System.out.println();
m*=2;
}
}


Hope this may help...
[ March 31, 2008: Message edited by: Shivit Agarwal ]

Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Think carefully about m*=2 for a moment.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Scroll down its the 1b I am doing not the 1a

Doooh. Sorry
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Originally posted by Tim LeMaster:
Think carefully about m*=2 for a moment.


You are right.

m*=2; is like a deadly virus. Thank You.

I have also overlooked the spaces case.There will a one space between two terms everytime. So little modification.


int len = args[0].length();

m =80/len -1; // -1 will take care of spaces
int i=0;

for(i=0;i<100;i++)
{
System.out.print(+args[0]+" ");
if(i++=m)
{ System.out.println();
i=0;
}


Originally posted by Christophe Verre:

Doooh. Sorry


Mate,thats the Q. we are talking about.
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Originally posted by Tim LeMaster:
Ask yourself:

How many times will the outer loop execute?
How many times will the inner loop execute for each outer loop iteration?
nameCount = consoleWidth/nameLength forgets what part of the output?


How many times will the outer loop execute? 100
How many times will the inner loop execute for each outer loop iteration?
Depends on the length on name I guess.
nameCount = consoleWidth/nameLength forgets what part of the output? no idea I am affraid.
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Originally posted by Tim LeMaster:
Think carefully about m*=2 for a moment.


I don't really understand what m*=2 does exactly, this is the problem my maths is weak, in fact I am having trouble understand how the code works that uses it.
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Originally posted by Shivit Agarwal:


if(test++==m)
{ System.out.println();
m*=2;
}
}



Being one with a need to understand the code rather than just getting the answer correct could you explain what the if(test++==m) and the m*=2 actually means.
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Lastly thanks all for your replies any one suggest a good place to learn the math I guess I will need to update my skills there before I continue programming =(
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

most of this isn't really complicated math, it's complicated syntax of the language.

a common requirement is to add one to a variable - i.e. you're trying to count something. you could say every time - "take the value of i, add one to it, and assign that value back to i".

since this is done SO OFTEN, the people who created Java designed a shortcut into the language... the ++ operator. so whenever you see something like , you can think of that as "add 1 to i".

another common thing done in programs is to take a variable, and add/subtract/multiply/something else it by something, and store that back in the original variable. there is a shortcut for this as well. so, instead of writing, you can write see if you can now explain what those two pieces in your question mean...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Originally posted by Stuart Smith:


I don't really understand what m*=2 does exactly, this is the problem my maths is weak, in fact I am having trouble understand how the code works that uses it.


Ok mate here I go...

m*=2 is equivalent to m=m*2;
Now let say m=1, then what will happen for the above expression..

for Loop
1: m=1*2=2
2: m=2*2=4 = 2^2 (^ stands for power)
3: m=4*2=8 = 2^3
4: m=8*2=16 = 2^4
...
Likewise

Now what will happen whn it will reach 20 round of loop, m will become
m=2^20 which is a very vast value. Now just think what will happen when loop terminate at 100. It may crash your program(error).

Hope you got something atleast.
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Right so I do not need the inner loop that makes sense to me now.

nameCount = consoleWidth / nameLength;

Should be

nameCount = consoleWidth / nameLength -1; ?

To account for the space I think.

Going to play with this some more to fully copmrened the output in a visual form, thank you all for the help like I said been stuck on this a while now although I have managed to get the next few lessons done with no problem including the Leap year test.

Thanks again everyone for helping understand what to me was complicated but in fact is very basic =/
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
I wasn't going to say anything - but as you changed the title to resolved I have to say the calculation "nameCount = consoleWidth / nameLength -1;" is a poor estimation of the space required at best. Here are some examples.

nameCount = 80/4 - 1; ==19 So a four character name will be printed on the line 19 times - that will actually take up 94 characters. You should only print 16 per line.

nameCount = 80/14 - 1; ==4 So a fourteen character name will be printed on the line 4 times - that will only take up 59 characters. It should be printed 5 times.

Personally I think the best solution for this does not try to determine how many to print per line, but just prints 100 hundred of them keeping track of how many characters have been printed and adding a new line when printing the next set would go beyond the number of columns.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
@Tim LeMaster

You are correct. I just put the concept to give the OP an idea that it could be done differently.

Anyway your logic was better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment Java-1b (Resolved)
 
Similar Threads
Generating random letters
How to print the * like this shape.
do while loop
Hashing Storage