• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm new to Java and I don't really know what I'm doing, please help!  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my question for my weekly task:

WRITE "Enter student name==> "
READ name
WRITE "Enter mark for the student==> "
READ mark
IF mark is less than 50 THEN
grade is assigned “F”
ELSE IF mark is less than 65 THEN
grade is assigned “P”
ELSE IF mark is less than 75 THEN
grade is assigned “C”
ELSE IF mark is less than 85 THEN
grade is assigned “D”
ELSE
grade is assigned “HD”
ENDIF
WRITE “The grade for ” + name + “ is ” + grade

Implementation
Create a class Week3 and a main method and also create Scanner objects as per question 2.
Read in the student name and mark (out of 100).
Declare a String object grade to store the grade string within the if statements.
Use nested if…else if statements to assign the correct grade string.
Finally output the grade as per the examples above
Follow the above pseudo code, enter a line or two of code and compile, always ensure you are
working with clean (error free) code.

This is what I've done, it doesn't really work as intended (I've literally been doing this for like 6 hours, no joke).



Thanks!!

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

If you don't understand what you are doing, then take small steps to gain understanding bit by bit. Don't try to learn too much at the same time, because it will be overwhelming.

The first thing you could do is organize your code better and understand the basic syntax and structure of Java programs.

Right now your code looks messy. Why do you have a { and } in lines 9 and 10, why did you write those lines like that? Your code is not formatted properly, the indentation is messy and makes it hard to read. Why are the constants in lines 15-19 starting in the leftmost column, and why in lines 23-40 is the indentation growing by 4 spaces every line?

Why is there a { in line 23? Why do you have a { and } in lines 38 and 39?

Your code doesn't seem complete. The braces { and } should always pair up. I don't see a closing brace } for the main method (the opening brace is in line 5) and for the class (the opening brace is in line 3).

So, first make sure your code is organized so that it's easy to read, and make sure that it compiles without errors.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't write so much code before running it to see if it works. Write the code that prints the first prompt. Compile, and run it. make sure it works.
Then add the code to get the user input for the first prompt. Compile and run. Test it. You may want to write a couple lines you'll later throw away that proves that at this point, you have the correct input.
keep going. Get to the point where you add the code for "if mark less than 50". Test that. test it a LOT.  Test edge cases (what if they enter 50? what if they enter -97?  what if htey enter "fred"?
Once that works, add the code for the "less than 65". Test that. test it a LOT.  Test edge cases (what if they enter 50? what if they enter -97?  what if htey enter "fred"? What if they enter 65? etc.

only add one piece at a time, and get it to work before you add the next piece.

if you only add 2 lines, and now there's an error, you can focus in on those two lines. If you add 100 lines and now have an error, it's MUCH harder to find.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A part of learning to write software is learning how to read requirements. Your requirements mention two variables: mark and grade. Mark is supposed to be a numeric value (e.g. 74), and grade is supposed to be a String (e.g. "P"). It also says you should "READ mark". I don't see that in your code. It says to assign "F" to grade. I don't see that in your code. You have grade (which should have been 'mark') as type double, nowhere in the requirements does it say the a grade may be fractional. Why not use an int?

P.S. To get the mark from your Scanner you can use nextInt().
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the biggest problem in your code is that all of your logic seems to be jammed into the main() method. Around here, we think that MainIsAPain (←click that, it's a link).  Your main() method should be small and it should be given the sole task of controlling the start and end of your program. All other logic not related to starting and ending the program should be delegated to either objects or other more focused methods.

One other thing to note: You only ever need to associate ONE Scanner variable with System.in. In your code, you create multiple Scanner objects that are associated with System.in, which is unnecessary since there's only one System.in object in a running Java program. I would recommend declaring a Scanner that's associated with System.in like this instead:

Then, throughout your whole program, you just use the globally available keyboard object to get input from the keyboard.

It's best to think of Scanners as being associated with their input source. Normally, there's only one keyboard on a computer, so if you want to scan the keyboard for input, you associate ONE Scanner object with System.in, which is associated to the keyboard device by default.

If you have String value that you want to scan, you'd associate a separate Scanner to that. If you have a file you want to scan, you'd associate a Scanner to that. If you have multiple files or multiple Strings that you want to scan for input, you'd associate one Scanner for each file or string. That is, if you wanted to scan File1.txt and File2.txt, you'd create one Scanner with the new Scanner("File1.txt") and assign that to one Scanner variable and then do new Scanner("File2.txt") and assign that to another Scanner variable.

Does that make sense?
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instructions wrote:Follow the above pseudo code, enter a line or two of code and compile, always ensure you are
working with clean (error free) code.

These are very good advises, seems you ignored them.

caleb Sm wrote:I've literally been doing this for like 6 hours, no joke

And what do you expect, all be done? That is normal, and it will take another 72 hours if you want to get a good mark. What you got as a messy code as been mentioned early, that is exactly 6 hours of work, you need more effort to make it nicer. So, roll up your sleeves, there are more stuff to come.

Please fix formatting, indentation and post code again, so we could move possibly forward.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Style: You need proper indentation. 'if' should be indented the same amount as its matching 'else'. Also the statement(s) below the if or else should be enclosed in braces.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!