d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Java Help Fg Tip > Title
12Next
Add Reply New Topic New Poll
Member
Posts: 2,139
Joined: Aug 20 2014
Gold: Locked
Nov 21 2014 08:27pm
Error reading int data, MIN_VALUE value returned.

error when trying to request example; 5.4
works if u enter 5

This post was edited by 5678 on Nov 21 2014 08:27pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 08:29pm
5.4 isn't an int
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 08:44pm
Quote (5678 @ Nov 21 2014 09:30pm)
100 FG if u can help me

Here is my code:



      //Variables
    int num1, num2;
   
    //Get Numbers
    System.out.print("\nEnter the first number: ");
    num1 = Keyboard.readInt();
    System.out.print("Enter the second number: ");
    num2 = Keyboard.readInt();
   
    //Decimal Format
    DecimalFormat fmt = new DecimalFormat(".000");
   
    //Bigger or Smaller
    if(num1 > num2){
    System.out.println("\nThe first number is larger than the second number");
    System.out.println(num1 + fmt.format(num1)+ " - second number");
    System.out.print(num2 + fmt.format(num2)+ " - first number");
    }
 
else if(num2 > num1){
  System.out.println("\nThe second number is larger than the first number");
  System.out.println(num2 + fmt.format(num2)+ " - second number");
    System.out.print(num1 + fmt.format(num1)+ " - first number");


Don't PM me with this crap.

If you want to input 5.4, then you shouldn't be reading in integers. Read in doubles.
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Nov 21 2014 08:59pm
You are reading only integers. 5.4 is a double.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 09:28pm
So, I have been trolling people who have done this in the past. Anyone who blatantly begs for code, or shows no interest in learning will get code from me. But it will be insanely ridiculous int he amount of abstraction and complexity it offers for the most mundane of tasks. A few weeks ago, I did someones assignment for reading in a couple unique numbers from keyboard. However, I abstracted it in a way that you could configure how many numbers, the range of numbers, and then abstracted it more to where you could basically swap out any interface with another and get completely new functionality from the code.

So, this guys assignment is pretty much a derivative of that assignment I did before. So, I am going to show you how easily good code can be tweaked and repurposed if you design it well the first time. I had to make a few minor adjustments. Mainly, before I made my NumberReaders based on the primitive int. Well, this was slopy. I should have made them more abstract. Now they work off the abstract Number class, which now I can read ANY type of number I want. Which means, I can modify it to read in doubles like this guy wants. What follows will be an expaination of the code
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 09:36pm
Code

public interface INumberReader
{
void readNumbers();
}


SO we start off with the base INumberReader interface. This exposes one method to readNumbers. This allows for any application that wishes to be a NumberReader to specify how it will read these numbers, without any of its comsumers carryign about the implementation details. The idea is simple: someone wants you to read numbers, they don't care how. Just read them.

Code
public interface INumberReaderFactory
{
INumberReader createNumberReader(INumberReaderConfig config);
}


Next we will use a Factory pattern to generate the NumberReaders. This lets a consuming application create a new NumberReader without carrying about how. It isn't a NumberReader expert. It doesn't need to know how to make one, so we will supply it an interface that will expose a createNumberReader method so that they can get a NumberReader whenever they want without needing to know implementation details.


Code
import java.io.InputStream;
import java.io.PrintStream;

public interface INumberReaderConfig
{
Number getMaximumNumber();
Number getMinimumNumber();
int getAmountOfNumbers();
InputStream getInputer();
PrintStream getPrinter();
}


Now, there are some things a NumberReader needs. But all NumberReaders are not the same. So let's make those things configurable so that we can be more dynamic with how our NumberReader will work. The things a NumberReader needs in order to do its job is the range of numbers it can input, the amount of numbers it can input, and then of course HOW it is going to input. This means we can control whether they are inputed from the console, a file, or any datasource really. We can just give it a stream to the input or output source.

Code
import java.io.InputStream;
import java.io.PrintStream;

public class StaticDoubleReaderConstants
{
public static final Double MAXIMUM_NUMBER = Double.MAX_VALUE;
public static final Double MINIMUM_NUMBER = Double.MIN_VALUE;
public static final int AMOUNT_OF_NUMBERS = Integer.MAX_VALUE;
public static final InputStream INPUTER = System.in;
public static final PrintStream PRINTER = System.out;
}


Code
import java.io.InputStream;
import java.io.PrintStream;

public class StaticDoubleReaderConfig implements INumberReaderConfig
{
public Number getMaximumNumber() { return StaticDoubleReaderConstants.MAXIMUM_NUMBER; }
public Number getMinimumNumber() { return StaticDoubleReaderConstants.MINIMUM_NUMBER; }
public int getAmountOfNumbers() { return 2; }
public InputStream getInputer() { return StaticDoubleReaderConstants.INPUTER; }
public PrintStream getPrinter() { return StaticDoubleReaderConstants.PRINTER; }
}


Next we have the implementation of the NumberReaderConfig. This one is for a DoubleReader, but not just a DoubleReader, these is for a StaticDoubleReader. All of these values are static and not changed. If we ever want to change them, we just need to update the constants in the constants class.

To be continued.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 09:46pm
Code
import java.io.*;
import java.util.Scanner;

public abstract class NumberReaderBase implements INumberReader
{
protected Scanner in;
protected PrintStream out;
protected Number maximumNumber, minimumNumber;
protected int amountOfNumbers;

public NumberReaderBase(INumberReaderConfig config)
{
readConfig(config);
}

private void readConfig(INumberReaderConfig config)
{
in = new Scanner(config.getInputer());
out = config.getPrinter();
maximumNumber = config.getMaximumNumber();
minimumNumber = config.getMinimumNumber();
amountOfNumbers = config.getAmountOfNumbers();
}

protected Number getMaximumNumber() { return maximumNumber; }
protected Number getMinimumNumber() { return minimumNumber; }
protected int getAmountOfNumbers() { return amountOfNumbers; }

protected abstract Number readNumber(String msg);

protected abstract boolean isValidNumber(Number number);

protected void printLine(String line)
{
out.println(line);
}

}


Now we are getting into the implementation of a NumberReader. We will start with an abstract base. Each NumberReader will be different, but there are some stuff each NumberReader must do. We will encapsulate that in the base, and expose abstract methods to be implemented by whichever concrete class we create later to give it unique functionality. The things every NumberReader will do is expose some input and output methods to its children and read its config file, which is supplied via dependency injection through the constructor. Notice that this abstract class doesn't deal with any specific Number. It is a base for ANY type of NumberReader.

Code
public abstract class DoubleReader extends NumberReaderBase
{

public DoubleReader(INumberReaderConfig config)
{
super(config);
}

protected Number readNumber(String msg)
{
Double number = 0.0;

do
{
out.println(msg);
number = in.nextDouble();

} while(!isValidNumber(number));

return number;
}

protected boolean isValidNumber(Number number)
{
return number.doubleValue() >= minimumNumber.doubleValue() && number.doubleValue() <= maximumNumber.doubleValue();
}
}


Next we have our implementation for a DoubleReader. But there can be many types of DoubleReaders, so again this is just an abstract class to establish the base for which all DoubleReaders will use. This one establishes what is a valid Number, and how to read a Number. Note that now we are dealing with Doubles instead of Numbers. The base does not implement readNumbers, as that is a more specific operation that other concrete DoubleReaders will implement.

Code

public class ComparableDoubleReader extends DoubleReader{

public ComparableDoubleReader(INumberReaderConfig config)
{
super(config);
}

public void readNumbers()
{
Double d1, d2;

d1 = (Double)readNumber("Enter the first number");
d2 = (Double)readNumber("Enter the second number");

if(d1 > d2) out.println("Number 1 is greater than Number 2");
else if(d1 < d2) out.println("Number 1 is less than Number 2");
else out.println("The Numbers are equal");

}

}


Here we have our concrete ComparableDoubleReader. This implementation implements the abstract readNumbers method. It provides the implementation for this specific kind of DoubleReader.

Code
public class ComparableDoubleReaderFactory implements INumberReaderFactory
{
public INumberReader createNumberReader(INumberReaderConfig config)
{
return new ComparableDoubleReader(config);
}
}


And of course, our consumers will want to be able to create these types of DoubleReaders, but won't necesarily know of care how they are made. So let's give an implementation of the INumberReaderFactory we created earlier so that we can create some ComparableDoubleReaders

More to follow....
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 09:52pm
Now it is time to set up our application to read some numbers

Code
public abstract class NumberReaderApplicationBase
{
private INumberReaderFactory numberReaderFactory;
private INumberReaderConfig numberReaderConfig;

public NumberReaderApplicationBase(INumberReaderFactory numberReaderFactory, INumberReaderConfig numberReaderConfig)
{
this.numberReaderFactory = numberReaderFactory;
this.numberReaderConfig = numberReaderConfig;
}

public void execute()
{
INumberReader nr = numberReaderFactory.createNumberReader(numberReaderConfig);

nr.readNumbers();
}
}


We start with an abstract NumberReaderApplicationBase. This will accept a factory and a config file from its contructor. It's one method, execute, simply creates a NumberReader with the NumberReaderFactory using the NumberReaderConfig, and then calls readNumbers. Note how everything is using interfaces, and is completely abstract and generic. ANY NumberReaderApplication can use this base.

Code
public class ComparableDoubleReaderApplication extends NumberReaderApplicationBase
{
ComparableDoubleReaderApplication()
{
super(new ComparableDoubleReaderFactory(), new StaticDoubleReaderConfig());
}
}


Now we have a concreate implementation of a NumberReaderApplication. This simply supplies the implementations of the factory and config. Now, in a perfect world, you would use some dependency injection framework to handle this at runtime, but for the sake of simplicity we are just going to manually inject them like this. Notice this is the FIRST time we have every specifically used an actual implementation of any of these interfaces. The entire NumberReader Framework above this point was strictly interfaces.


Code
public class ComparableDoubleReaderApplicationDriver
{
public static void main(String[] args)
{
new ComparableDoubleReaderApplication().execute();
}
}


And last but not least, a simple driver which constructs a new application and executes it.


And before you ask me, OP, the above solution does EXACTLY what you want it to do.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 21 2014 10:18pm
1) inputer and printer are both streams inside INumberReaderConfig, so it's harder to build a GUI or web app or web service to leverage the core code
2) configuration options should really come from a config file. what happens when OP deploys this to his linux box and decides he wants to change the number range? he'd have to recompile the code instead of altering a config file
3) i don't see any logging anywhere. what happens when we notice a lot of CPU usage on the box and narrow it down to this program, and notice someone added a concrete DoubleReader implementation which is flawed and loops infinitely? if we have log files, we can set up alerts to notify us of the errors, or at least help trouble shoot the issue without having to get out a debugger.
4) you're not providing any feedback to the user or log if the user doesnt enter a double. you just keep on trucking. our customers will keep calling in tier 1 support asking why it doesnt work when they type in "pi" or "e" or "two" or "(1+2)". and since there's no documentation, they're gonna bring in tier 2 support, and guess what? they know the developer's phone number.
5) elbows too pointy
6) no multi-language support. your strings should all come from an NLS file so developers dont have to recode it just to support spanish, french, korean, etc.


2/10 would not bang.

This post was edited by carteblanche on Nov 21 2014 10:47pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 10:24pm
Quote (carteblanche @ Nov 21 2014 11:18pm)
1) inputer and printer are both streams inside INumberReaderConfig, so it's harder to build a GUI or web app or web service to leverage the core code
2) configuration options should really come from a config file. what happens when OP deploys this to his linux box and decides he wants to change the number range? he'd have to recompile the code instead of altering a config file
3) i don't see any logging anywhere. what happens when we notice a lot of CPU usage on the box and narrow it down to this program, and notice someone added a concrete DoubleReader implementation which is flawed and loops infinitely? if we have log files, we can set up alerts to notify us of the errors, or at least help trouble shoot the issue without having to get out a debugger.
4) you're not providing any feedback to the user or log if the user doesnt enter a double. you just keep on trucking. our customers will keep calling in tier 1 support asking why it doesnt work when they type in "pi" or "e" or "two" or "(1+2)". and since there's no documentation, they're gonna bring in tier 2 support, and guess what? they know the developer's phone number.
5) elbows too pointy


2/10 would not bang.


You are absolutely right. This was the initial commit. Those features have already been prioritized and added to the backlog. The development team will start working on a maintenance release next sprint.

This post was edited by Minkomonster on Nov 21 2014 10:24pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll