1

So I only VERY recently got into what people call "coding", especially Java. Here's what I've made with my teeny tiny hands. Basically, you input a year, and it converts it into centuries:

import java.util.Scanner;

    public class somemaths {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);
        System.out.println("Please insert a year:");
        int str = sc.nextInt();
    int calc1 = (str+99)/100;

    if (str>0) {
        System.out.print("This is the " + calc1);
        int unity = calc1%10;

        if (unity>=4)
            System.out.print("th century. I'm sure of it!");

        if (unity==3)
            System.out.print("rd century. I'm sure of it!");

        if (unity==2)
            System.out.print("nd century. I'm sure of it!");

        if (unity==1)
        System.out.print("st century. I'm sure of it!");
        }
    else
        System.out.print("Please don't input negative numbers :c");

    }
}

Questions are:

1) Eclipse tells me that 'sc' is never closed. What is this about?

2) Is the code itself okay-ish?

3) This is probably the noobiest question I'll ever ask, but how can I create a window with a dialog and a text box (as in, a box where you can input some numbers), and then show the result in another dialog window? I've vaguely heard of JOptionPane.showMessageDialog before, but I just don't know how to apply it here.

Thanks a bunch!

Staidanom
  • 21
  • 6
  • I don't understand #1 - `sc` *isn't* closed, so what's the question? – EJoshuaS - Stand with Ukraine Sep 26 '17 at 14:17
  • sc is your scanner input stream. you will create memory leaks by leaving this open. this seems to be more of a code review question – andrewdleach Sep 26 '17 at 14:17
  • Thing is, I don't understand what Eclipse means by "isn't closed". As I said, I'm quite new to this, so excuse me if I sound like I don't understand what's going on ^^" – Staidanom Sep 26 '17 at 14:20
  • Streams should be closed when they're not needed anymore. the class `Scanner` has a method for closing. Call that at the end of your program – ParkerHalo Sep 26 '17 at 14:21
  • 1) eclipse is warning you about [this method](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()). If you don't call it after you finish using it (or use try-catch with resources) is like using the oven and leaving it open; 2) it depends on your needs; 3) This is too broad to get an answer here. Find a tutorial about swing or JavaFX instead. – Juan Carlos Mendoza Sep 26 '17 at 14:22

3 Answers3

3

You should try and focus your questions a bit more on Stack Overflow to get better answers. Asking three questions in one is expecting a lot. Also your second question is better suited to code review stack exchange. Your third question is also quite broad.

Question 1:

A Scanner object needs to be closed once you're finished with it. Call sc.close() at the end of your program to do this.

Question 2:

Your variable names aren't as good as they could be. If you want to ensure your code is 'complete', you should look into unit testing and write a comprehensive suite of tests to ensure your code covers all cases. E.g. you will have a test case which takes the date 1980, and ensures the correct output of the 20th century is given.

Question 3:

You should look into Java Swing for creating a simple GUI. There are lots of tutorials out there.

dahui
  • 2,128
  • 2
  • 21
  • 40
  • Thanks a lot! That's actually pretty useful. – Staidanom Sep 26 '17 at 14:24
  • No problem. Welcome to Stack exchange, if you want to show an answer is useful (like mine and HyperNeutrinos answers), then you can give them an upvote and mark an answer as the 'Accepted Answer' (click the tick). – dahui Sep 26 '17 at 14:25
2

sc is never closed

Since Scanners use input streams, they will open the stream to use them. They don't automatically close so to ensure safe and clean termination, after the last input from sc, call sc.close(). Technically since it's System.in nothing bad happens if you don't but you should close it anyway.

Is the code itself simple/complete enough?

Go to Code Review if your code works and you want to make it better in terms of efficiency, et cetera. However, I will tell you that you should probably be doing something like this:

if (str > 0) {
    System.out.print("This is the " + calc1);
    int unity = calc1%10;
    String century;
    if (unity == 1) century = "st";
    else if (unity == 2) century = "nd";
    else if (unity == 3) century = "rd";
    else century = "th";
    System.out.print(century + " century. I'm sure of it!");
else
    System.out.print("Please don't input negative numbers :c");
}

JOptionPane#showMessageDialog

Official Documentation of javax.swing.JOptionPane

For your purpose, you probably want:

JOptionPane.showMessageDialog(
    null, // This is the parent frame; if unspecified, it will make a new window
    "the output goes here", // This is the output you want to show to the user
    "some title" // You can specify the title
    // This will use the defaults to display a notification (not an error or a confirm) with the default icon. Check the docs for more information
)
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
0

I've modified your java code to clean it up a bit. I'll provide some explanations as to what I changed an why I did so.

sc.close();

Your first problem is caused by not properly calling the close() method of the scanner object named sc. You can find some other helpful posts on stack overflow (such as this one) on why this is essential, but you will need to understand how your programs utilize memory and how the Scanner object in particular utilizes it. For now, I would just always make sure that you close all scanner objects once they are done being utilized.

if (unity>=4)
    ....
if (unity==3)
    ....
if (unity ==2)
    ....

Instead of using an if statement for each case of what the unity variable equals, you should be using either if else statements or a switch statement. In your program, you do not want any possibility of the other if statements executing. It might not seem obvious in your example, but if this type of code was within a much larger project, you or someone else could vary easily modify the unity variable in between one of your if statements and change the behavior of your output. This is a good practice to get into as you are learning to program, and is important once you start working on larger projects. Always ask the question, "What if 5 years from now someone looked at my code?" :) I would also try to follow a consistent "coding standard". Be consistent in your spacing and when to use curly braces. This makes your code much more readable.

import java.util.Scanner;

public class somemaths {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please insert a year:");
        int str = sc.nextInt();
        sc.close();
        int calc1 = (str+99)/100;

        if (str>0) {
            System.out.print("This is the " + calc1);
            int unity = calc1%10;

            if (unity>=4){
                System.out.print("th century. I'm sure of it!");
            } else if (unity==3){
                System.out.print("rd century. I'm sure of it!");
            } else if (unity==2){
                System.out.print("nd century. I'm sure of it!");
            } else if (unity==1) {
                System.out.print("st century. I'm sure of it!");
            } else {
                System.out.print("Please don't input negative numbers :c");
            }
        }
    }
}
Preston Martin
  • 2,789
  • 3
  • 26
  • 42