• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Can not get my program to loop, and my top row is "null"

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want my array to be empty after it is set, so " ". I have it set up almost right, but for some reason the first row still comes up "null". I also cannot figure out how to loop my program. I ask for input to fill element, so user will say "row 1, column 5, fill with an 'x'". I want that to store, and rerun the program until the user chooses to quit. I attempted a 'do' and 'while' loop, but it turned into an infinite loop. Any help would be much appreciated. Yes, this is homework, but I have been at it for about 8 hours and have searched and read a lot to get to where I am. Here is a link to my program, hopefully this is the best way to ask a question on here.



 
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why you'd have an infinite loop. OK, line 42 says "while(true)" which would be an infinite loop if it weren't for "break" on line 41, and a call to quitProgram() on line 39.
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that your print method sets all the array elements to a space after printing the first (zero-ith) row.
 
Lost javaCoder
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did fix the infinite loop, (i'm not sure how to update my code to show the current status).
My only problem now is seeing the elements in the array to  blank " " , instead of the default "null"
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 65 & 66 clear the matrix to spaces, but I'm guessing that these lines shouldn't be in the print method.
 
Sheriff
Posts: 16103
268
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
Don't use System.exit() to terminate your program: this should be used only in abnormal situations. The user choosing to exit the program is NOT an abnormal situation that warrants the use of System.exit().

See this for more information: https://www.baeldung.com/java-system-exit
 
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a nice article from Baeldung, but I prefer not to use System.exit() full stop. Baeldung is right (he always know what he is talking about) to recommend using exceptions to deal with abnormal situations.

i'm not sure how to update my code to show the current status

Please don't update anything. Please post the new code in a new post.
Agree about not using an infinite loop. What you should do is move the "Q" option to the loop continuation predicate: ... while (!input.equalsIgnoreCase("Q")); Then the program terminates naturally. Delete lines 37‑40. By the way, I think the break; (line 41) is always executed regardless of the option you choose.


...and, welcome to the Ranch
 
lowercase baba
Posts: 12973
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lost javaCoder wrote:I did fix the infinite loop, (i'm not sure how to update my code to show the current status).


just put the updated version in a new post.  We prefer you not change the original post, as it makes it harder for someone else to follow the thread, and makes replies look non-sensical.
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic