[Java] IO class + Doagain class

Page 1 of 2 12 LastLast
Results 1 to 25 of 34
  1. #1
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    [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.

    One more thing~
    Must save all these things as .java~!!
    Last edited by ihatehaxor; 22-10-09 at 06:20 PM.


  2. #2
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    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.
    Last edited by Generic230; 21-10-09 at 01:07 AM.

  3. #3
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Generic230 View Post
    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.

  4. #4
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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

  5. #5
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    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..

  6. #6
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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);
        }

  7. #7
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    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.

  8. #8
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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.
    Last edited by Generic230; 21-10-09 at 01:29 AM.

  9. #9
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    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.

  10. #10
    Account Upgraded | Title Enabled! MjClarke1 is offline
    MemberRank
    Jun 2006 Join Date
    London, OntarioLocation
    644Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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.

  11. #11
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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 View Post
    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.
    Last edited by Generic230; 21-10-09 at 02:05 AM.

  12. #12
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    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.

  13. #13
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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.

  14. #14
    Account Upgraded | Title Enabled! MjClarke1 is offline
    MemberRank
    Jun 2006 Join Date
    London, OntarioLocation
    644Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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.

  15. #15
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Generic230 View Post
    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?

  16. #16
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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
    Code:
    public class IO{
    (which is what your file is called)

  17. #17
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Generic230 View Post
    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?

  18. #18
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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...

  19. #19
    Account Upgraded | Title Enabled! MjClarke1 is offline
    MemberRank
    Jun 2006 Join Date
    London, OntarioLocation
    644Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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.

  20. #20
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by MjClarke1 View Post
    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

  21. #21
    Account Upgraded | Title Enabled! MjClarke1 is offline
    MemberRank
    Jun 2006 Join Date
    London, OntarioLocation
    644Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Generic230 View Post
    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

  22. #22
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    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..

  23. #23
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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 View Post
    It's the EXACT same as yours... I just fixed the spacing and imports...
    ^

    Quote Originally Posted by Generic230 View Post
    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.
    ^
    Last edited by Generic230; 21-10-09 at 02:26 AM.

  24. #24
    Omega ihatehaxor is offline
    MemberRank
    Apr 2008 Join Date
    JerseyLocation
    5,461Posts

    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...

  25. #25
    Account Upgraded | Title Enabled! MjClarke1 is offline
    MemberRank
    Jun 2006 Join Date
    London, OntarioLocation
    644Posts

    Re: IO class + Doagain class

    Quote Originally Posted by Ace View Post
    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



Page 1 of 2 12 LastLast

Advertisement