Help on a Java lab for school

Hello! I would like help on a Java lab for school, I'm really confused on it :((
61 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @soybean 𝜗𝜚! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
What exactly is the issue/What confuses you?
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
im new to coding so idk the vocab much bc i was absent for sometime so idk how to answer these questions and i need to do my own experiment for this lab
No description
bassus
bassus2mo ago
Okay let's go over this step by step
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
TY MUCHH I did some of it would u like to see but my experiment for it does not work
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
No description
bassus
bassus2mo ago
1) A variable's datatype is defined in it's declaration. Declaration means basically definition, so the first time the variable is written down in code: <datatype> <variablename>; or <datatype> <variablename> = <something>; (in the second variant we also initialize the variable. That means we give it its first value) 2) The Scanner is a class from the Java API. Just google "java se25" or "java se24", depends on the version you are using, and then click on the "overview" site (https://docs.oracle.com/en/java/javase/24/docs/api/index.html). There you can enter the name of your class (Scanner) in ther searchbar, find it and then read up on what it does 3) Those questions can be answered by reading about those methods in the same source as in "2)" 4 & 5) You basically have two types of so called control (flow) structures in imperative programming (like Java, Python, C, C++, C#...). For one you have decision statements, like if & else if & else or switch & case. With these 2 thingys you can choose the only execute one "path" in your code, you make a choice in an either do this or do that sense. If the if-condition is met you only execute the following block (in the {} and nothing else). Same thing goes for else if and the cases in switch (though their code blocks are not in {} but in : ... break;) . Now if no if/else if or case is true, the else or default block is executed (it has no condition). Example:
int y = 5;
String s;
if (y % 2 == 0) {
s = "even";
} else { //if we had more than two options for the result of y % 2, we probably would need other else ifs, to check for that. y % 2 is either 0 or 1, so it is not needed in that case here.
s = "odd";
}
int y = 5;
String s;
if (y % 2 == 0) {
s = "even";
} else { //if we had more than two options for the result of y % 2, we probably would need other else ifs, to check for that. y % 2 is either 0 or 1, so it is not needed in that case here.
s = "odd";
}
alternatively with switch case:
int y = 5;
String s;
switch (y % 2) {
case 0:
s = "even";
break;
default:
s = "odd";
break;
}
int y = 5;
String s;
switch (y % 2) {
case 0:
s = "even";
break;
default:
s = "odd";
break;
}
Now as second type of control structuers you have loops. Loops allow you to execute a block of statements. There are also two (and a half) types for that. while-loops allows you to repeat the block as long as the condition in the () of the while is true. This is checked again and again after each iteration. The for-loop is very similar to that, but it allows you to iterate with an index, so basically for a certain amount of times over the block. Example:
int i = 0;
while (i < 10) {
System.out.println("Hello!");
i++;
}
int i = 0;
while (i < 10) {
System.out.println("Hello!");
i++;
}
Or in for:
for (int i = 0; i < 10; i++) { //initialize running index; breaking condition; update index after each iteration
System.out.println("Hello!");
}
for (int i = 0; i < 10; i++) { //initialize running index; breaking condition; update index after each iteration
System.out.println("Hello!");
}
Lastly, what did i mean by 2 and a half control structures for loops? There are also so called do-while-loops. How do they work? Example:
int i = 42;
do {
System.out.println("Bye!");
} while (i < 10); // this isn't true, but the loop is executed once, because the while is only checked after the first iteration!
int i = 42;
do {
System.out.println("Bye!");
} while (i < 10); // this isn't true, but the loop is executed once, because the while is only checked after the first iteration!
You wanna do your experiment with x wit if-else if-else, not with 3 ifs btw
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
oh ok
bassus
bassus2mo ago
What doesn't work about it?^^
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
it doesn't print "Hello Mr.Taylor!" into the console
bassus
bassus2mo ago
Enter 3 and send a screenshot, what happens
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
okii
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
No description
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
i didnt enter 3 bc it wont run bc of the error
dan1st
dan1st2mo ago
you tried to use the variable z which you didn't set to any value initializing = setting a variable to a value
bassus
bassus2mo ago
But z was never used, or am i stupid? At least from what i can see here
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
OHHHHHHH i needed to create my own variable and set it to value
bassus
bassus2mo ago
Can you like show the code again pls
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
so i can show him i tried experimenting to make the program run smth new sureee
bassus
bassus2mo ago
Without the overlaying error msg
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
No description
bassus
bassus2mo ago
Yeah, you never read a value into z So how should the 3 you input end up in z?
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
ohhh ok i see now loll one sec
bassus
bassus2mo ago
You could for one change the first two ifs to
if (x == 1) {
System.out.println("Hello World!");
} else if (x == 2) {
System.out.println("Hallo Mundo!");
} else {
System.out.println("No useful input given");
}
if (x == 1) {
System.out.println("Hello World!");
} else if (x == 2) {
System.out.println("Hallo Mundo!");
} else {
System.out.println("No useful input given");
}
Cleaner code.
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
YEAH OK ILL DO THAT RN AND SEE
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
No description
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
like this?
ayylmao123xdd
ayylmao123xdd2mo ago
in the last if you got z instead of x if z == 3
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
oh ty for pointing that out lemme fix it
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
No description
bassus
bassus2mo ago
You cooked the syntax xD If you wanna do it like that, you have to remove the "old" ifs
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
ohhhhh ok one sec tyyyy im so stupid, but what r the "old ifs"
bassus
bassus2mo ago
For example, why is the else if (x == 2) { followed by another if (x == 2) {? And also, speaking about the syntax, why is there just an opening { before the if (x == 3) {, why is this if even still here? No offence, but do you understand, what you are doing here?
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
not really, im like really really new to coding lol
bassus
bassus2mo ago
Read this again^^ If it doesn't help tell me^^
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
okay!
bassus
bassus2mo ago
Also, do you know what curley brackets do in Java/C/C++ etc.
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
no, wait tbh i was absent for the first 3 to 4 blockdays of this course so not rlly
bassus
bassus2mo ago
{ } defines a scope. A scope can be a class ,like HelloWorldChoice, or a method, like the main or a control flow structure, like if, while, for, switch, else if, else In these scopes you can do stuff (gross oversimplification, but enough for now) Now, check your code again, anything wrong with the scopes? Hint: you open a scope with { and close it with }. You also ideally use a TAB to indents between opening and closing {}, so you have the scopes nicely formatted No need to think about the functionallity of classes, methods or control flow structures! Just tell me if the scopes look like they should
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
ohhh yeah i removed some of the scopes
bassus
bassus2mo ago
What do you mean by that
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
the extra ones, the errors told me to remove some of them
bassus
bassus2mo ago
Yeah that fixed it, but what was the origin of that mistake in the first place? To be more direct, what did those extra brackets fuck up?
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
the syntax? is that what ur asking?
bassus
bassus2mo ago
Yeah that also, but basically you destroyed the hierarchy of the scopes Like how they are stacked into each other The main-scope is inside of the HelloWorldChoice-scope. The if-, else- and else if-scope is in the main-scope. You added some if-scopes inside of the else if-scope but forgot the closing } for that, boom code kaputt. Was that explanation understandable
bassus
bassus2mo ago
This thingy for example
No description
bassus
bassus2mo ago
The if doesn't get a proper closing } Or the strange opening { in front of the if (x==3). This opens some scope, but it has no key word in front of it, so it's absolutely illegal xD
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
Omg thank so much for helping, ur a life saver loll
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
bassus
bassus2mo ago
No problem
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
:))
bassus
bassus2mo ago
So, how does the fixed code look like?
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
omg i didnt see ur text 😭
No description
ayylmao123xdd
ayylmao123xdd2mo ago
😱 should look like this
if (x == 1) {
// english system out
} else if (x == 2) {
// spanish system out
} else {
// no useful input given print
// this runs every time the number isnt 1 or 2
}
if (x == 1) {
// english system out
} else if (x == 2) {
// spanish system out
} else {
// no useful input given print
// this runs every time the number isnt 1 or 2
}
the line with dashes is where u put system out println try this
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
okay!
bassus
bassus2mo ago
Why are there 2 opening brackets? Why is there nothing happening in the if? Why are the last two System.out.println in the same case, the else if-case?
No description
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
one sec loll
bassus
bassus2mo ago
Luls😂
soybean 𝜗𝜚
soybean 𝜗𝜚OP2mo ago
oh imma switch to doing chem but ill come back to this and ill show it to u guys
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?