mercredi 11 mars 2015

Custom Methods In Java Package



Help, I am brand new to programming, and I have an assignment I'm stuck on, and I'm hoping someone can help me over a hump. The assignment is: Using Netbeans, write a class called IO, in a package called util, containing the methods: getInput(String), getConsoleInput(String), showMessage(String), and showMessage(String,String). The method getInput(String) will be through GUI, the method getConsoleInput will input/output through the console and return a Scanner object. The second showMessage will accept a title for the output dialog. Then write a program using the IO class, but in a separate file and in the default package. Your program will first ask the user to enter 2 integers. If both are positive, print the sum. Then it will ask for 2 real numbers(float or double); if only one is negative, print the product of the 2 numbers. If both are negative, print the quotient. Use nested if's where possible. Output must be formatted. And all input/outputs must be done through the methods you created. All output is to be done through console and dialog. I THINK I've written my methods correctly, but when my program codes calls them, I get multiple exceptions AFTER I respond to the dialog box:



Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Assignment4.main(Assignment4.java:18)


Java Result: 1 BUILD SUCCESSFUL (total time: 9 seconds)


This is the code for my methods:



package Util;

import java.awt.Font;
import java.util.Scanner;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;

/* getInput asks for input from GUI, then outputs to a Dialog box.
*getConsoleInput uses the console to ask for input, then outputs
*it to the console.
*/
public class IO {

public static Scanner getInput( String prompt){
String s = JOptionPane.showInputDialog(prompt);
return new Scanner(s);
}
/* Defines the getConsoleInput method, which requests input from the
*console, then returns output through the console.
*/

public static Scanner getConsoleInput(String prompt ) {
System.out.println(prompt);
Scanner input = new Scanner(System.in);
return input;
}
/*Defines the showMessage method, which outputs to a Dialog Box
*/

public static void showMessage(String s){
System.out.println(s);
JOptionPane.showMessageDialog(null, s);
}
/*Defines the overloaded showMessage method, which outputs to a Dialog Box,
but changes the text from whatever is initially the default.
*/
public static void showMessage(String output, String title) {
System.out.println(output);
JTextArea jta = new JTextArea(output);
jta.setFont(
new Font("MonoType Corsiva", Font.BOLD, 18));
JOptionPane.showMessageDialog( null, jta);
}
}


This is what I have so far for the program:



import java.util.Scanner;
import Util.IO;

public class Assignment4 {
/* Calls the getInput method, requesting the user enter 2 integers,
*then compares them to determine whether or not they're both
*positive. If so, it returns the sum.
*/

public static void main(String[] args) {
Scanner scan = IO.getInput("Enter 2 integers");
int x = scan.nextInt();
int y = scan.nextInt();

if (x > 0 && y > 0)
IO.showMessage(String.format("The sum is: %6.2d", (x + y)));


}
}



Aucun commentaire:

Enregistrer un commentaire