Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Just got started with Java - looking for feedback!

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Just got started with Java - looking for feedback!

    Hi

    About to start a maths degree and what with the computer age opening up at the business-side I figure having programming experience will help on a CV. So, yesterday morning I started reading Java: A Beginner's Guide (The one you can preview from Oracle's website). Today I wrote my first proper program: this iterative code to print out the digital root of a number (you keep summing its digits until you get a 1-digit number). Here is my code:

    package test;
     
    public class DigitalRootTest {
    	public static void main(String args[])
    	throws java.io.IOException {
    		System.out.println("Type in a whole number and press ENTER.");
    		int sum = 0, num = (int) System.in.read();
    		boolean failedinput = false;
    		while(num != 13) { //I put this here to break the loop of skimming characters off the end of the buffer since a carriage return is 13 in Unicode
    			if(num < 48) { //The digits 0-9 span the range 48-57 in Unicode
    				System.out.println("Invalid input.  Please enter a whole number.");
    				failedinput = true;
    				break;
    			} else if(num > 57) { //The digits 0-9 span the range 48-57 in Unicode
    				System.out.println("Invalid input.  Please enter a whole number.");
    				failedinput = true;
    				break;
    			} else {
    				num -= 48; //Converting the digits from Unicode integers to their actual values
    				sum += num; //Totalling up the digits as they're skimmed from the buffer
    				num = (int) System.in.read(); // getting a new character
    			}
    		}
    		System.out.println(sum);
    		if(failedinput == false) {
    			while(sum > 9) {
    				int i, lastdigit, worksum = sum, lim = 1 + (int) Math.log10(sum);
    /* i is a counting variable
        worksum is just a copy of sum that I can operate on safely without messing up the end result
        lastdigit is a variable just to hold the last digit of the current worksum as I work on it
        lim is a limit to be imposed on the for loop below, which is just the number of digits of the sum */
    				sum = 0; //reset the sum
    				for(i = 1; i <= lim; i++) {
    					lastdigit = worksum % 10; //get the last digit of the worksum
    					sum += lastdigit;
    					worksum -= lastdigit;
    					worksum = (int) worksum/10; //Note
    				}
    				System.out.println(sum);
    			}
    		}
    	}
    }

    Note: I realised here I can omit the line where I take the last digit off the worksum since casting to an integer would lop this off anyway.

    Can I get some feedback on this code? Can anyone spot a case in which it would fail (I haven't found one yet through putting in some large-ish numbers)? Also, how can I extend the code to send it back to the start after completing an operation? Would sticking it in an infinite for loop be okay or is that frowned upon?
    Last edited by Halyon; September 16th, 2014 at 10:02 AM. Reason: Edited adding // tags to make my logic clear


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Just got started with Java - looking for feedback!

    That seems like a goofy way to get input. Why don't you use a Scanner instead?

    Putting this inside a loop (such as a do-while loop) is reasonable. Just make sure you give the user a way to exit the loop, that way it's not an infinite loop anymore.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Just got started with Java - looking for feedback!

    Haven't met a scanner yet. I've basically got up to chapter 3 of that book (so I've covered if, for, while, dowhile, switch, else, and the basic structure of the language). Would you mind showing me the syntax?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Just got started with Java - looking for feedback!

    This is the first result for googling "java scanner tutorial": Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Just got started with Java - looking for feedback!

    Thanks - what is the purpose of the * after the import of java.io?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Just got started with Java - looking for feedback!

    Google is your best friend for these types of questions. Googling and reading tutorials and API documentation is a huge part of programming.

    This is the first result for googling "java importing" and it contains the answer to your question: Using Package Members (The Java™ Tutorials > Learning the Java Language > Packages)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Just got started with Java - looking for feedback!

    It is a wildcard much like someone you is looking for photos may search for *.jpg

    You import everything in java.io

  8. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Just got started with Java - looking for feedback!

    Right, I sort of understand that - I appreciate the use of google but sometimes it's nice to get an informal explanation. Thank you all for your help

Similar Threads

  1. Help Getting Started With Java (tutorial)
    By D.K in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: October 27th, 2013, 10:56 AM
  2. Feedback on java assignment
    By MadMulken in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2013, 03:20 PM
  3. Replies: 0
    Last Post: April 5th, 2013, 10:20 AM
  4. Getting started with java spring
    By Doyle Raymond in forum Web Frameworks
    Replies: 4
    Last Post: December 19th, 2012, 09:56 AM
  5. newbie: Getting started with java, need help on this
    By umairrockx in forum Java Theory & Questions
    Replies: 2
    Last Post: July 21st, 2011, 05:47 AM

Tags for this Thread