-
[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?
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
So then tell me, How would I use this in my script?
Make a file called Main.java place my code in there, compile, run.
Or replace
Code:
public class Main {
with
(which is what your file is called)
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Generic230
Make a file called Main.java place my code in there, compile, run.
no not that, how would I use it in a program.
like IO.getString
what do you type, to use this function?
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
no not that, how would I use it in a program.
like IO.getString
what do you type, to use this function?
It's the EXACT same as yours... I just fixed the spacing and imports...
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
no not that, how would I use it in a program.
like IO.getString
what do you type, to use this function?
Well because its a class, you'd probably have to
IO test = new IO();
test.getString() whatever the params are.
-
Re: IO class + Doagain class
Quote:
Originally Posted by
MjClarke1
Well because its a class, you'd probably have to
IO test = new IO();
test.getString() whatever the params are.
They're all static, just like his.
So, IO.getString() & whatever his otehr ones are
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Generic230
They're all static, just like his.
So, IO.getString() & whatever his otehr ones are
Only if you public static main void(String[] args) is in the same file, if you're calling functions from a different java file you have to create the object
-
Re: IO class + Doagain class
wait wth, epic fail. You didn't change anything.... Trying to trick me or something? You just deleted the extra spaces..... That doesn't make any difference, wth was the point..
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
wait wth, epic fail. You didn't change anything.... Trying to trick me or something? You just deleted the extra spaces..... That doesn't make any difference, wth was the point..
I already told you that, I just removed your useless imports and fixed spacing. It just makes your code neater, having messy code with bad spacing is bad practice.
Quote:
Originally Posted by
Generic230
It's the EXACT same as yours... I just fixed the spacing and imports...
^
Quote:
Originally Posted by
Generic230
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?
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, this is just a class. You don't ever have to even use this code again.. So who cares if there are extra spaces. As long as it works, thats all it matters...
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Dude, this is just a class. You don't ever have to even use this code again.. So who cares if there are extra spaces. As long as it works, thats all it matters...
It's just proper programming practice, if you always write messy code, you'll stay in that mindset and always write messy code.
If you use proper spacing etc.. you'll always do it properly as you practice
-
Re: IO class + Doagain class
In Dr.Java, it can already properly space it for me.
Just do ctrl + a
and click on tab.
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Dude, this is just a class. You don't ever have to even use this code again.. So who cares if there are extra spaces. As long as it works, thats all it matters...
Code:
public static int getInt(String str) {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return 0;
}
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Ace
Dude, this is just a class. You don't ever have to even use this code again.. So who cares if there are extra spaces. As long as it works, thats all it matters...
Well, first its practice, if you use bad coding practice on one file, you might use it on another.
Second if you ever need to review that file or implement something else, it will take more time to do(probably just some seconds lol).
Third people wont just copy/paste what you posted, they will try to understand it before use, so using proper programming practice is better.
---------
On a side note, man, calm down, accept what people say or dont post it, dont expect everyone to say "I loved it, thanks" because this is very hard to get.
Also you started the flamming on your own thread :\
-
Re: IO class + Doagain class
Just some tips
- allow the method to throw the exception and catch up where it's called.
- Don't use JOptionPane.showinputdialog for getting a yes/no answer, use showOptionDialog.
- Why bother using compareTo when you can do equals
-
Code:
String word;
word = IO.getString("Enter word") ;
You should learn to instantaneously create a variable and set a value to it
- Why do we have system.out.println() everywhere.. Use logger and set the logging level to finest if you want to debug the class so other people using your class won't have to see all the things you're printing to the screen (im assuming you want to debug it o__o)
- Assuming you want to make DoAgain Singleton
public Doagain(){}
Should be private
- Your println method is redundant.
-
Re: IO class + Doagain class
Okay I found out how to do the Do command.
Goodluck~
-
Re: IO class + Doagain class
Jay be trollin' Any ways, your classes are 99% useless, the 1% use is well, to assist me in crying when people don't know what they're doing.
-
Re: IO class + Doagain class
Quote:
Originally Posted by
Theoretical
Jay be trollin' Any ways, your classes are 99% useless, the 1% use is well, to assist me in crying when people don't know what they're doing.
Hi Jacob! :)
I think this thread is over anyways.
-
Re: [Java] IO class + Doagain class
PHP Code:
import javax.swing.JOptionPane;
public class DoAgain {
public static boolean ask() {
String again;
do {
again = JOptionPane.showInputDialog("Again? y or n? ");
System.out.println("Again? y or n? " + again);
} while (again.equals("y") && !again.equals("n"));
return again.equals("y");
}
}
PHP Code:
import javax.swing.*;
public class IO {
public static int getInt(String message) {
int num = 0;
try {
num = Integer.parseInt(JOptionPane.showInputDialog(message).trim());
System.out.println(message + ": " + num);
} catch (NumberFormatException e) { // test for bad input
System.err.println(e.toString()); // error message
System.exit(0); // stop program
}
return num;
}
public static double getDouble(String message) {
double num = 0;
try {
num = Double.parseDouble(JOptionPane.showInputDialog(message).trim());
System.out.println(message + ": " + num);
} catch (NumberFormatException e) { // test for bad input
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);
}
}
My 2 versions.
Generic knows way more Java than you do, just saying.
-
Re: IO class + Doagain class
Quote:
Originally Posted by
MjClarke1
Only if you public static main void(String[] args) is in the same file, if you're calling functions from a different java file you have to create the object
You don't need to create a new object if all your doing is accessing static methods from the class.