Learning Java, How to output file "showing evidence of the program's required functionality" - Page 2 - Overclock.net - An Overclocking Community

Forum Jump: 

Learning Java, How to output file "showing evidence of the program's required functionality"

Reply
 
Thread Tools
post #11 of 65 (permalink) Old 11-16-2017, 04:50 PM
Software 'Engineer'
 
jakethesnake438's Avatar
 
Join Date: Oct 2008
Location: Noo Zolond
Posts: 875
Rep: 53 (Unique: 48)
#1 The '5' value you are passing into this array is actually the size of the array, not the content
So you just created an array which has 5 objects of type 'int', the default value of int is zero, which is why you got that result
Code:
int[] userAge3 = new int[5];
is basically the same as going
Code:
int[] userAge3 = new int[]{0,0,0,0,0};
That creates a new array with five zeros

With the above code, i'm sure you can figure out how to create an array with one value of 5

#2 Have a look at the java documentation for 'copyOfRange'
Code:
Arrays.copyOfRange(source, 3, 7)
This creates a new array from the old one that starts at index 3 and ends at index 7

Have a go changing the index parameters you are passing into copyOfRange and see how the result changes

Is this still broken?
Xeon machine
(13 items)
CPU
Xeon E5-2667V2
Motherboard
HP Z620 2012
GPU
Sapphire RX 580 4GB
RAM
32GB 1333MHZ
Hard Drive
2x 256GB SATA SSD
Operating System
Windows 10
Operating System
MacOS Mojave
Monitor
Dell P2418D 1440p
Monitor
Dell U2414H 1080p
Keyboard
Apple Kbd
Mouse
Steelseries QCK
Mouse
Logitech G602
Audio
Senheiser HD449
▲ hide details ▲
jakethesnake438 is offline  
Sponsored Links
Advertisement
 
post #12 of 65 (permalink) Old 11-16-2017, 05:06 PM - Thread Starter
New to Overclock.net
 
TheReciever's Avatar
 
Join Date: Nov 2009
Posts: 11,316
Rep: 434 (Unique: 322)
Quote:
Originally Posted by jakethesnake438 View Post

#1 The '5' value you are passing into this array is actually the size of the array, not the content
So you just created an array which has 5 objects of type 'int', the default value of int is zero, which is why you got that result
Code:
int[] userAge3 = new int[5];
is basically the same as going
Code:
int[] userAge3 = new int[]{0,0,0,0,0};
That creates a new array with five zeros

With the above code, i'm sure you can figure out how to create an array with one value of 5

#2 Have a look at the java documentation for 'copyOfRange'
Code:
Arrays.copyOfRange(source, 3, 7)
This creates a new array from the old one that starts at index 3 and ends at index 7

Have a go changing the index parameters you are passing into copyOfRange and see how the result changes

Lovely!

Arrays.copyOfRange(source, 3, 7)

So, when using copyOfRang, source is what its calling, 3 is the index to begin pulling and 7 is the end. Because index 3 is where it begins to pull, when println for output index 3 becomes 0 when calling.

Thank you so much smile.gif

Biased rPGA and MXM 3.0A/B Elitest.
TheReciever is offline  
post #13 of 65 (permalink) Old 11-16-2017, 05:12 PM
Overclocker
 
JackCY's Avatar
 
Join Date: Jun 2014
Posts: 9,990
Rep: 339 (Unique: 240)
Code:
class HelloWorld {
        public static void main( String[] args ) {
                String names = "Peter, John, Andy, David";
                System.out.println(String.join(",", names.split(", ")));
        }
}
Code:
Peter,John,Andy,David

https://ideone.com/0TXbPr

And oh man does it remind my how much I dislike Java. So damn verbose.

Second has been answered right, it's size not a value. You have to know what you're actually setting. [number] is either a pointer or a declaration of an array of number size, depends where it is.
print(array[5]) ... array[5] is a pointer to an object and returns the object
whatever = array[5] ... is a creation of an array of size 5 named whatever where whatever is a pointer and array[5] is a pointer to an allocated memory space, array[5] also allocates the memory space as you have given it a size 5, without size it will have no memory space, of course Java will "hide" all this and is made "dumb" so you don't have to think of it this way but it makes more sense as that is what is happening in a way under the hood although Java wraps and bloats everything a lot.
JackCY is offline  
Sponsored Links
Advertisement
 
post #14 of 65 (permalink) Old 11-20-2017, 09:44 PM
New001
 
Join Date: Feb 2016
Posts: 2,668
Quote:
Originally Posted by JackCY View Post

And oh man does it remind my how much I dislike Java. So damn verbose.

Haha it can be a bit. Makes for some pretty readable code though. Lambdas in java8 have helped a bit.
spinFX is offline  
post #15 of 65 (permalink) Old 11-20-2017, 10:37 PM - Thread Starter
New to Overclock.net
 
TheReciever's Avatar
 
Join Date: Nov 2009
Posts: 11,316
Rep: 434 (Unique: 322)
The hardest part for me is motivation.

I dont think I have any interest in Java or at least what I can attempt to do with it since anything I thought I could apply it to was actually Javascript or C++/C# etc

But I jut have to get through it.

I am in the next section now, Formatting outputs and converting integers

Biased rPGA and MXM 3.0A/B Elitest.
TheReciever is offline  
post #16 of 65 (permalink) Old 11-26-2017, 11:16 PM - Thread Starter
New to Overclock.net
 
TheReciever's Avatar
 
Join Date: Nov 2009
Posts: 11,316
Rep: 434 (Unique: 322)
BUMP!

Updated OP with new problem, waiting to hear your insights!

Thanks Guys <3

Biased rPGA and MXM 3.0A/B Elitest.
TheReciever is offline  
post #17 of 65 (permalink) Old 11-26-2017, 11:19 PM
New001
 
Join Date: Feb 2016
Posts: 2,668
Quote:
Originally Posted by TheReciever View Post

The hardest part for me is motivation.

I dont think I have any interest in Java or at least what I can attempt to do with it since anything I thought I could apply it to was actually Javascript or C++/C# etc

But I jut have to get through it.

I am in the next section now, Formatting outputs and converting integers

Don't lose hope haha, sometimes i find motivation hard as well, but I'll just tell myself, "just do one thing, one small thing, then reassess". Usually that does the trick, once I get going, Im going. Will take a look at the updated OP, but i reckon make new threads for each question, I only stumbled back on this thread randomly tongue.gif

Also, I think once you get a bit further you'll start to feel a bit warmer towards java, it isn't a bad language today.
spinFX is offline  
post #18 of 65 (permalink) Old 11-26-2017, 11:30 PM
New001
 
Join Date: Feb 2016
Posts: 2,668
Quote:
Originally Posted by TheReciever View Post

Hey guys!
Code:
System.out.printf("The answer for %.3f divided by &d is %.2f." , 5.45, 3, 5.45/3);
!

The mistake is just the ampersand (&) symbol.
System.out.printf uses string formatting
ie.
%s is wildcard for strings
%d is wildcard for integer
and %f is wildcard for floats (decimals etc).

So you are trying to insert three values into the string, 5.45 (float), 3 (integer) and 5.45/3 (float)
Therefor you are using %f (float), %d (digit/ integer) and %f (float)
++ Plus you are adding in precision for the floats (.3 : 3 decimal places & .2 : 2 decimal places)

So your line should be:
Code:
System.out.printf("The answer for %.3f divided by %d is %.2f." , 5.45, 3, 5.45/3);

Another example of string formatting would be
Code:
String myString = String.format("%s %s %s!", "My", "formatted", "string");
System.out.println(myString);
-> would output 'My formatted string!'
spinFX is offline  
post #19 of 65 (permalink) Old 11-27-2017, 12:07 AM - Thread Starter
New to Overclock.net
 
TheReciever's Avatar
 
Join Date: Nov 2009
Posts: 11,316
Rep: 434 (Unique: 322)
Quote:
Originally Posted by spinFX View Post

Don't lose hope haha, sometimes i find motivation hard as well, but I'll just tell myself, "just do one thing, one small thing, then reassess". Usually that does the trick, once I get going, Im going. Will take a look at the updated OP, but i reckon make new threads for each question, I only stumbled back on this thread randomly tongue.gif

Also, I think once you get a bit further you'll start to feel a bit warmer towards java, it isn't a bad language today.

I have about 3 weeks to get this course done. I shouldve allowed for more time but I am only just now coming out of a harsh brush with severe depression. I have to justify most of my actions to commit to them and sadly this wasnt something I could "justify" so I sat on it.

Im not working right now so I think 3 weeks to pass a test and a small Java project should be ok. I was debating sharing my project here but I think that may be cheating. If I get stuck starting it though I may present some of it. I in general have issues starting projects of any nature.

Quote:
Originally Posted by spinFX View Post

The mistake is just the ampersand (&) symbol.
System.out.printf uses string formatting
ie.
%s is wildcard for strings
%d is wildcard for integer
and %f is wildcard for floats (decimals etc).

So you are trying to insert three values into the string, 5.45 (float), 3 (integer) and 5.45/3 (float)
Therefor you are using %f (float), %d (digit/ integer) and %f (float)
++ Plus you are adding in precision for the floats (.3 : 3 decimal places & .2 : 2 decimal places)

Another example of string formatting would be
String myString = String.format("%s %s %s!", "My", "formatted", "string");
System.out.println(myString);
-> would output 'My formatted string!'

I JUST realized this after looking over the code 3 more times, once corrected it outputted as expected lol.

One more thing, is there any particular reason why 3 backslashes dont work?



EDIT!

Also, for the lines of code with printf, what can I do to make that line appear in its own line? I know the println will do that but if I am using the printf command already is there a way to still do this?

Biased rPGA and MXM 3.0A/B Elitest.
TheReciever is offline  
post #20 of 65 (permalink) Old 11-27-2017, 12:17 AM
New001
 
Join Date: Feb 2016
Posts: 2,668
Quote:
Originally Posted by TheReciever View Post

I have about 3 weeks to get this course done. I shouldve allowed for more time but I am only just now coming out of a harsh brush with severe depression. I have to justify most of my actions to commit to them and sadly this wasnt something I could "justify" so I sat on it.

Im not working right now so I think 3 weeks to pass a test and a small Java project should be ok. I was debating sharing my project here but I think that may be cheating. If I get stuck starting it though I may present some of it. I in general have issues starting projects of any nature.
I JUST realized this after looking over the code 3 more times, once corrected it outputted as expected lol.

One more thing, is there any particular reason why 3 backslashes dont work?



EDIT!

Also, for the lines of code with printf, what can I do to make that line appear in its own line? I know the println will do that but if I am using the printf command already is there a way to still do this?

"\n" is a new line character

so if you wanted printf's on a new line do something like ::
Code:
System.out.printf("\nThis is on a new %s", "line");

Triple back slash doesnt work because backslash is an escape character when used inside a string.

So for example if i have this string::
Code:
"I like apples"

Now i want to put the word apples in quotes, but i cannot do this:
Code:
"I like "apples""
, that will appear as two strings ("I like " & "") as well as a badly formatted line
So instead have to do
Code:
"I like \"apples\""

Same if you wanted to use a backslash in a string::
Code:
"This is string 1\20"
is going to try to escape the 2 character, so we need to escape the slash to print it:
Code:
"This is a string 1\\20"

So you can see that an uneven number of backslashes will result in an unwanted escape.
spinFX is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off