• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I need Help with my Java code.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


----jGRASP exec: java ProGamerTest
What would you like to do with the ProGamer Team.

Type 'View' to view the current roster.
Type 'Search' to see an indiviual's KD Ratio.
Type 'Average' to see the teams average KD.
Type 'Change' to replace a member with a new one.

Change

Enter the name of the Team Member whose KD you want to see.

Shroud

The gamer you entered is Shroud and has a KD of 10.15

The new gamers name is
Apex
and their kd is
12.25
Are you finished, Y or N




Thats the outcome, thats where it stops, it wont let me type Y or N to continue the do-while, so i cant continue testing.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

STart by moving all that code out of the main() method. It is not intended for writing business logic (nor games logic). That code all belongs in different methods, probably in different classes. Also such a long method is a maintenance nightmare.
Don't use the == operator on Strings. Your test in line 101 is guaranteed to go wrong.

What have you been taught Scanner#nextLine() does? What does your book say Scanner#nextLine() does? Because it doesn't.
  • 1: Explanation and possible solution here.
  • 2: Alternative solution: change nextLine() to next() in line 99.
  • 3: Other alternative solution, much better than the first two, here. See how it handles nextLine().
  •  
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would suggest you set all that code aside for the moment.  Start a new, temporary project where you write a loop that does one thing - asks "are you finished, Y or N".

    if the user enters "N", it should ask again.  if the user enters "Y", it should quit.  Then you need to decide what to do if the user enters "n" or "y".  or "Yes" or No" or "YES" or "NO" or "Not quite" or....or decide that you don't care about anything else.

    Once you have THAT loop working, you can then see about having that loop do OTHER stuff, by adding your current code back in.  I'd probably break down what you have into more methods, but that's probalby a lesson for another day.
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Welcome to the Ranch

    Start by moving all that code out of the main() method. It is not intended for writing business logic (nor games logic). That code all belongs in different methods, probably in different classes. Also, such a long method is a maintenance nightmare.
    Don't use the == operator on Strings. Your test in line 101 is guaranteed to go wrong.



    I have to rewrite the =='s using the Equals function, I am a new coder, just taking my first Java College Class and am having trouble understanding the use of more than 1 method. Is it neatness or is there an actual reason.

    Everything works now except for the Change Case after I type in the final info, it doesn't allow me to answer the yes or no question.
     
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Trystin Garcia wrote:am having trouble understanding the use of more than 1 method. Is it neatness or is there an actual reason.


    It's more than just neatness. Imagine putting everything in your house in just one room, say the garage. Kitchen utensils, clothes, books, TV, bath products, everything. In the garage. First, it would be a very messy garage. Also it's very inconvenient. Every time you need a new towel or a need to cook dinner, you have to go to the garage to do it. Each thing has its logical place in the house. A bed goes in the bedroom, pots go in the kitchen, towels in the bathroom, vehicles in the garage.

    Now, if you don't put different tasks into different methods that serve a specific purpose but instead put all your code in one method, you end up with the programming equivalent of all the stuff in your house being kept in the garage.

    So, it's about organization. A well organized program is easy to maintain, easy to read, and most importantly, easier to test. Neatness is just a happy side-effect of good organization.
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Compare how much easier this would be to read and comprehend vs. the giant block of code that you have written:

    Then you'd have smaller methods for doView(), doSearch(), etc., with each subtask being done in its own special "room" in your program.

    Trust me, you'll save a lot of brain cells doing it this way.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Trystin Garcia wrote:. . .I have to rewrite the =='s using the Equals function, . . .

    Not Equals but equals please, but call it a method, not a function. People will understand you better if you use the correct jargon.

    it doesn't allow me to answer the yes or no question.

    Yes it does, but you aren't presenting it with yes or no. Have you changed your code? If so, please show us it. Did you understand what I linked to about nextLine() in my earlier post?
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Is my new code, im going to do the new methods, but each case allows input and gives an awnser, then allows for you to type N and do it again. While the Change Case finishes then doesnt let you type N, and just ends the code
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Trystin Garcia wrote:. . . While the Change Case finishes then doesnt let you type N, and just ends the code

    So you haven't solved that problem. Have you looked at the links I gave you?
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah I have, yet its kinda hard to understand, so you want me to make it got from int to a line?
    continuation = keyboard.nextInt()
    s.nextLine()
     
    Ranch Hand
    Posts: 127
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Trystin Garcia wrote:


    Try to explain what this line does with your own words - hint: it does not what you think it does ...
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It runs once, then if the variable continuation is said "N" or "n" by the end of the do then it will do the code again.
     
    Bob Winter
    Ranch Hand
    Posts: 127
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Are you sure about that?
    At the very last line you just print an empty line. How about printing continuation to see whats in it?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Trystin Garcia wrote:. . . hard to understand

    Please tell us what you don't understand and we shall try to explain it, just as Bob Winter is already trying to help you.

    Bob Winter wrote:. . . got from int to a line? . . .

    No.

    How about printing continuation to see whats in it?

    Have you tried that? Make sure to wrap it in quotes to make it easier to read: System.out.printf("Continuation = \u201c%s\u201d%n", continuation);
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I changed it to print continuation but It came out blank, but the thing is, the first 3 cases work correct, then allow me to input Y or N, but the final case, doesn't allow input afterward. It asks the question then stops, there is no place for me to input Y or N if I use the first 3 cases, I can do the constant loop, but the final case won't allow me.
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:Compare how much easier this would be to read and comprehend vs. the giant block of code that you have written:

    Then you'd have smaller methods for doView(), doSearch(), etc., with each subtask being done in its own special "room" in your program.

    How would I give the all 4 methods the arrays to use.
    I did what you said.



    So this is my second test file, how would I pass the info of the array to all 4 of them

     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is a typical problem when trying to read text from a Scanner after you've just read in a numeric value.

    Do this to fix your issue:

    Once you add line 96 above, the nextLine() call on line 104 will stop to wait for your input. Right now, it's basically doing what line 96 above is doing, consuming the return after the double value that you typed in.
     
    Trystin Garcia
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok thanks
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sounds like you got the program running.  Care to share what you did?  Or are you done with code reviews for a while!
     
    What's wrong? Where are you going? Stop! Read this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic