[Java] IO class + Doagain class
For my school, we use these two classes.
I don't really know if these are releases, but here.
Okay.
Code for IO class.
Code:
import java.io.*;
import javax.swing.*;
public class IO{
public static int getInt(String message){
String s = JOptionPane.showInputDialog(message);
int num;
try{
num = Integer.parseInt(s.trim()); //convert String s to an int
System.out.println(message+": "+num);
}catch(NumberFormatException e){ //test for bad input
num=0;
System.err.println(e.toString()); //error message
System.exit(0); //stop program
}
return num;
}
public static double getDouble(String message){
String s = JOptionPane.showInputDialog(message);
double num;
try{
num = Double.parseDouble(s.trim()); //convert String s to an int
System.out.println(message+": "+num);
}catch(NumberFormatException e){ //test for bad input
num=0;
System.err.println(e.toString()); //error message
System.exit(0); //stop program
}
return num;
}
public static String getString(String message){
String s = JOptionPane.showInputDialog(message);
System.out.println(message+": "+s);
return s;
}
public static char getChar(String message){
String s = JOptionPane.showInputDialog(message);
if(s.length()==0){
System.out.println(message+": ");
return '\0';//null char
}else{
System.out.println(message+": "+ s.charAt(0));
return s.charAt(0); //first char
}
}
public static void println(String message){
System.out.println(message);
JOptionPane.showMessageDialog(null, message);
}
}
How to use :
First, save this anywhere (preferably in the folder where you save your java programs).
Second, you have to use the IO like this, Don't really know how to say it, so I'll show you.
Code:
public class MyFirstProgram {
public static void main(String[] args) {
String word;
word = IO.getString("Enter word") ;
System.out.println(word);
}
}
Okay, now to explain it. It's pretty much self - expanitory, unless you don't know anything about Java. The variable word is IO.getString <make note that the new class is IO.getString> with parentheses showing "Enter word" . This basically just lets you enter any word, without having to change the variable a million times. It can just be "word".
Try it, I can't really get a pic right now, so I'll get it later.
Now, DoAgain.
Code:
import javax.swing.*;
public class Doagain{
public Doagain(){}
public static boolean ask(){
return ask("Another? ");
}
public static boolean ask(String message){
String again;
do{
again = JOptionPane.showInputDialog(message+" y or n? ");
System.out.println(message+" y or n? "+ again);
}while(again.compareTo("y") !=0 && again.compareTo("n") !=0);
return again.compareTo("y") == 0;
}
}
Basically, just doAgain. Just like it says, the code just makes you do it again, basically you don't have to run a million times.
(This is good use with the IO.getString)
How to use,
Code:
public class MyFirstProgram {
public static void main(String[] args) {
String word;
do {
word = IO.getString("Enter word") ;
System.out.println(word);
}while(Doagain.ask()) ;
}
}
Okay, just put the bold stuff on, and that is really all to it.
Make sure the do {
Is always after you set all the variables.
Well, yea this is all the releases~
Have fun with it. :thumbup:
One more thing~
Must save all these things as .java~!!
Re: IO class + Doagain class
Your IO class is essential parsing a data type with a try catch warapped around it
P.S: I see errors.
Re: IO class + Doagain class
Quote:
Originally Posted by
Generic230
Your IO class is essential parsing a data type with a try catch warapped around it
P.S: I see errors.
Did you even try it?
cause it works for me noob.
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Did you even try it?
cause it works for me noob.
I thought there was a " at the start of the System.out.println, and why would I want a class that's entire point is to wrap try{} & catch{} around parsing something
Re: IO class + Doagain class
If you don't need it then don't use it.
It's not like this is just for you..
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
If you don't need it then don't use it.
It's not like this is just for you..
I'm just notifying you that it is near useless, you should get netbeans and click "Format", and use Fix Imports and only import the one class you need rather than importing 2 entire packages.
Code:
import javax.swing.JOptionPane;
public class Main {
public static int getInt(String message) {
String s = JOptionPane.showInputDialog(message);
int num;
try {
num = Integer.parseInt(s.trim()); //convert String s to an int
System.out.println(message + ": " + num);
} catch (NumberFormatException e) { //test for bad input
num = 0;
System.err.println(e.toString()); //error message
System.exit(0); //stop program
}
return num;
}
public static double getDouble(String message) {
String s = JOptionPane.showInputDialog(message);
double num;
try {
num = Double.parseDouble(s.trim()); //convert String s to an int
System.out.println(message + ": " + num);
} catch (NumberFormatException e) { //test for bad input
num = 0;
System.err.println(e.toString()); //error message
System.exit(0); //stop program
}
return num;
}
public static String getString(String message) {
String s = JOptionPane.showInputDialog(message);
System.out.println(message + ": " + s);
return s;
}
public static char getChar(String message) {
String s = JOptionPane.showInputDialog(message);
if (s.length() == 0) {
System.out.println(message + ": ");
return '\0';//null char
} else {
System.out.println(message + ": " + s.charAt(0));
return s.charAt(0); //first char
}
}
public static void println(String message) {
System.out.println(message);
JOptionPane.showMessageDialog(null, message);
}
Re: IO class + Doagain class
Retard, you messed up your code.
#1 there's an error.
Code:
File: C:\Users\Dan\Desktop\JavaStuff\IO.java [line: 55]
Error: C:\Users\Dan\Desktop\JavaStuff\IO.java:55: reached end of file while parsing
epic fail. there is no need to re-write my code, when it works fine..
So stop freaking spamming the thread.
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Retard, you messed up your code.
#1 there's an error.
Code:
File: C:\Users\Dan\Desktop\JavaStuff\IO.java [line: 55]
Error: C:\Users\Dan\Desktop\JavaStuff\IO.java:55: reached end of file while parsing
epic fail. there is no need to re-write my code, when it works fine..
So stop freaking spamming the thread.
Two things, I didn't rewrite it, second, I didn't paste the last } but no, apparently I broke it. :(
Code:
import javax.swing.JOptionPane;
public class Main {
public static int getInt(String message) {
String s = JOptionPane.showInputDialog(message);
int num;
try {
num = Integer.parseInt(s.trim()); //convert String s to an int
System.out.println(message + ": " + num);
} catch (NumberFormatException e) { //test for bad input
num = 0;
System.err.println(e.toString()); //error message
System.exit(0); //stop program
}
return num;
}
public static double getDouble(String message) {
String s = JOptionPane.showInputDialog(message);
double num;
try {
num = Double.parseDouble(s.trim()); //convert String s to an int
System.out.println(message + ": " + num);
} catch (NumberFormatException e) { //test for bad input
num = 0;
System.err.println(e.toString()); //error message
System.exit(0); //stop program
}
return num;
}
public static String getString(String message) {
String s = JOptionPane.showInputDialog(message);
System.out.println(message + ": " + s);
return s;
}
public static char getChar(String message) {
String s = JOptionPane.showInputDialog(message);
if (s.length() == 0) {
System.out.println(message + ": ");
return '\0';//null char
} else {
System.out.println(message + ": " + s.charAt(0));
return s.charAt(0); //first char
}
}
public static void println(String message) {
System.out.println(message);
JOptionPane.showMessageDialog(null, message);
}
}
I just clicked two buttons on netbeans to not make it have disgusting spacing.
Also proving you shouldn't be 'teaching' Java when you couldn't figure out a simple error.
Re: IO class + Doagain class
Um no there was no point to fix that error, because my fucking script works already. Do you not understand english.
There is no need to fix the damn code.
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Um no there was no point to fix that error, because my fucking script works already. Do you not understand english.
There is no need to fix the damn code.
Offtopic: Dude. Calm the fuck down, every thread I see you in all you do is try to bash Generic telling him he doesn't know java etc. He probably knows more than you, stop being a dick to people.
On topic: Alot of the class files from your class will most likely be useless as there is usually something already in java that does it. The reason your teacher might give you these, or force you to make your own is so you can figure out how things work instead of just using something premade and being clueless.
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Um no there was no point to fix that error, because my fucking script works already. Do you not understand english.
There is no need to fix the damn code.
I'm fixing your shitty spacing because it looks disgusting, you could check for any other changes, and I removed your useless imports, or do you not understand english?
Quote:
Originally Posted by
MjClarke1
Offtopic: Dude. Calm the fuck down, every thread I see you in all you do is try to bash Generic telling him he doesn't know java etc. He probably knows more than you, stop being a dick to people.
On topic: Alot of the class files from your class will most likely be useless as there is usually something already in java that does it. The reason your teacher might give you these, or force you to make your own is so you can figure out how things work instead of just using something premade and being clueless.
He's been coding Java(learning from school loool) for maybe a week, and decided he is better than anyone else.
Re: IO class + Doagain class
Dude do you even check your freaking works?
File: C:\Users\Dan\Desktop\JavaStuff\IO.java [line: 3]
Error: C:\Users\Dan\Desktop\JavaStuff\IO.java:3: class Main is public, should be declared in a file named Main.java
Just stop trying. You really suck.
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Dude do you even check your freaking works?
File: C:\Users\Dan\Desktop\JavaStuff\IO.java [line: 3]
Error: C:\Users\Dan\Desktop\JavaStuff\IO.java:3: class Main is public, should be declared in a file named Main.java
Just stop trying. You really suck.
Your file isn't named "Main.java" like mine is, dumb ass. Stop trying to prove me wrong, you really suck.
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Dude do you even check your freaking works?
File: C:\Users\Dan\Desktop\JavaStuff\IO.java [line: 3]
Error: C:\Users\Dan\Desktop\JavaStuff\IO.java:3: class Main is public, should be declared in a file named Main.java
Just stop trying. You really suck.
l2 find errors... It's pretty obvious what the problem is, the class is declared as Main
(public class Main) rename the .java file to main or change the class name to the current file.
Re: IO class + Doagain class
Quote:
Originally Posted by
Generic230
Your file isn't named "Main.java" like mine is, dumb ass. Stop trying to prove me wrong, you really suck.
So then tell me, How would I use this in my script?