Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Problem in merging two java classes

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem in merging two java classes

    Hi,

    I have two java codes Main.java and doinput.java, i wanted to have them into a single java file.I tried it like this
    import java.io.*;
    import java.util.StringTokenizer;
     
    class Main {
    	public class doinput {
     
    		static String s = " ";                
    		static DataInputStream stdin = new DataInputStream(System.in);
    		static StringTokenizer s_tok = new StringTokenizer(s);
     
    		public static int nextint() throws java.io.IOException {  
    		/* Will return the next integer from the Input Stream.
    		Assumes that at least one more integer
    		remains in the input stream
    		*/
    		int n;                               
     
    		while (s_tok.countTokens() == 0) {
    		s = stdin.readLine();       
    		s_tok = new StringTokenizer(s);
    		};
    		n = Integer.parseInt (s_tok.nextToken());
    		return n;
    		};
     
    		public static String nextsubstring() throws java.io.IOException{
    		/* Will return the next substring from the Input Stream.
    		Assumes that at least one more substring 
    		remains in the input stream
    		*/
    		String Str;
     
    		while (s_tok.countTokens() == 0) {
    		s = stdin.readLine();       
    		s_tok = new StringTokenizer(s);
    		};
    		Str = s_tok.nextToken();
    		return Str;
    		};
     
    	}
     
    	public static void main (String [] args) throws IOException {
    		int num, sum, count;
    		String num_string;
    		doinput myinput = new doinput();
     
    		System.out.println("PERFECTION OUTPUT");
     
    		num = myinput.nextint();
     
    		while (num != 0)  {
    		  // what is it?
    		  if (num != 1)
    			sum = 1;
    		  else sum = 0;
    		  count = 2;
    		  while (count < num)  {
    			if ((num % count) == 0) 
    			  sum = sum + count;
    			count = count + 1;
    			}
     
    		  System.out.print (num + " ");
    		  if (sum < num) System.out.println ("DEFICIENT");
    		  if (sum == num) System.out.println ("PERFECT");
    		  if (sum > num) System.out.println ("ABUNDENT");
     
    		  num = myinput.nextint();
    		  }
    		System.out.println("END OF OUTPUT");
    	}
    }

    i got these errors.

    Main.java:17: inner classes cannot have static declarations
                          static string s = " ";
    Main.java:18: inner classes cannot have static declarations
                          static DataInputStream stdin = new DataInputStream(System.in);
    Main.java:19: inner classes cannot have static declarations
                          static StringTokenizer s_ok = new StringTokenizer(s);
    Main.java:21: inner classes cannot have static declarations
                          public static int nextint() throws java.IOException {
    Main.java:36: inner classes cannot have static declarations
                          public static String nextsubstring() throws java.IOException {
    Main.java:56: non-static variable this cannot be referenced from a static context
                    doinput myinput = new doinput();

    can anyone tell/show me what to do to fix this?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: merging java classes

    Hello madkris and welcome to the Java programming forums.

    There were a few things wrong with the code. I've got rid of any reference to 'doinput' because all this code is now within the Main class.

    Try this:

    import java.io.*;
    import java.util.StringTokenizer;
     
    public class Main {
     
            static String s = " ";                
            static DataInputStream stdin = new DataInputStream(System.in);
            static StringTokenizer s_tok = new StringTokenizer(s);
     
            public static int nextint() throws java.io.IOException {  
            /* Will return the next integer from the Input Stream.
            Assumes that at least one more integer
            remains in the input stream
            */
            int n;                               
     
            while (s_tok.countTokens() == 0) {
            s = stdin.readLine();       
            s_tok = new StringTokenizer(s);
            };
            n = Integer.parseInt (s_tok.nextToken());
            return n;
            };
     
            public static String nextsubstring() throws java.io.IOException{
            /* Will return the next substring from the Input Stream.
            Assumes that at least one more substring 
            remains in the input stream
            */
            String Str;
     
            while (s_tok.countTokens() == 0) {
            s = stdin.readLine();       
            s_tok = new StringTokenizer(s);
            };
            Str = s_tok.nextToken();
            return Str;
            };
     
     
        public static void main (String [] args) throws IOException {
            int num, sum, count;
            String num_string;
     
            System.out.println("PERFECTION OUTPUT");
     
            num = nextint();
     
            while (num != 0)  {
              // what is it?
              if (num != 1)
                sum = 1;
              else sum = 0;
              count = 2;
              while (count < num)  {
                if ((num % count) == 0) 
                  sum = sum + count;
                count = count + 1;
                }
     
              System.out.print (num + " ");
              if (sum < num) System.out.println ("DEFICIENT");
              if (sum == num) System.out.println ("PERFECT");
              if (sum > num) System.out.println ("ABUNDENT");
     
              num = nextint();
              }
            System.out.println("END OF OUTPUT");
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: merging java classes

    works wonderfully.

    so i dont have to declare any other class when its under the Main class?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: merging java classes

    You wanted two classes into one. You only need to have one main class, there is no need to declare the other class in this case.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: merging java classes

    ahh right. Thanks for the help.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] merging java classes

    No problem
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SOLVED] merging java classes

    question: is there a way to have both classes in a single java file but with doinput class outside of the Main class? how?

    i get lost often with this pair of code still want to have them in a single java file or class or whichever is easier to do or understand.

    //autoedit.java
    import java.io.*;
     
    class autoedit {
        public static  void main(String[] arg) {
            String[] find = new String[10];
            String[] replace = new String[10];
            ACMIO in = new ACMIO("autoedit.in");
            PrintWriter out = null;
            try {
              out = new PrintWriter(
                      new BufferedWriter(
                        new FileWriter("autoedit.out"))); 
            }catch(Exception e) {
                System.out.println("can't open output");
            }
            int nrep = in.intReadln();
            while (nrep > 0) {
              for (int i = 0; i < nrep; i++) {
                find[i] = in.getLine();
                replace[i] = in.getLine();
              }
              String line = in.getLine();
              for (int i = 0; i < nrep; i++) { 
                int pos = line.indexOf(find[i]);
                while (pos >= 0) {
                  line = line.substring(0,pos) + replace[i] + 
                         line.substring(pos + find[i].length());  
                  pos = line.indexOf(find[i]);
                }
              }
              out.println(line);
              nrep = in.intReadln();
            }
            out.close();
        }
    }

    // ACMIO.java
    import java.io.FileReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.PushbackReader;
    import java.text.NumberFormat;
    public class ACMIO
    {
       private static ACMIO stdin = null; 
       private PushbackReader in;
     
       final static int  EOF = -1;      // Returned by in.read() at EOF
     
     
       /**
       * Construct an ACMIO to read from a file.
       * @param 
       *   <code>filename</code> file to read from.
       * <dt><b>Abort</b>
       *   if the file cannot be opened to read.
       **/
       public ACMIO(String filename) {
          try
          {
             in = new PushbackReader(new FileReader(filename));
          }
          catch (Exception e)
          {
            badExit("can't open file " + filename);
          }   
       }
     
       private ACMIO() {
             in = new PushbackReader(new InputStreamReader(System.in));
       }
     
       /**
       * Returns the <code>ACMIO</code> object that reads from <code>System.in</code>.
       * @param none
       * @return
       *   <code>ACMIO</code> object that reads from <code>System.in</code>
       **/
       public static ACMIO getStdin() {
            if (stdin == null)
                stdin = new ACMIO();
            return stdin;
       }
     
       /**
       * Read and discard whitespace: blanks, tabs, and newlines.
       * @param none
       **/
       public void skipWhitespace()
       {
          unread(pastWhitespace());   
       }
     
     
       /**
       * Read and discard blanks.
       * @param none
       **/
       public void skipBlanks()
       {
          int next = read();
          while (' ' == next)
             next = read();
          unread(next);   
       }
     
     
       /**
       * Read and discard the rest of the current input line.
       * @param none
       **/
       public void skipLine()
       {
          int next = read();
          while (next != '\n' && next != EOF)
             next = read();
       }
     
       /**
       * Read until the end of the current line.
       * @param none
       * @return
       *   a <CODE>String</CODE> with all characters other that '\r' or '\n' 
       *   until the end of the line, or end-of-file if that comes first.
       **/
       public String getLine()
       {
          StringBuffer buffer = new StringBuffer(80);
          int next = read();
          while (next != '\n' && next != EOF) {
             if (next != '\r') 
                 buffer.append((char)next);
             next = read();
          }
          return buffer.toString();
       }
     
       /**
       * Read a character.
       * @param none
       * @return
       *   a character read 
       * 
       * <dt><b>Abort</b>
       *   if tried to read at end-of-file.
       **/
       public char getChar()
       {
          int next = read();
          if (next == EOF) 
              badExit("expected char, not EOF!");
          return (char) next;
       }
     
       /**
       * Skip whitespace and read a character.
       * @param none
       * @return
       *   a character read that is not whitespace 
       * 
       * <dt><b>Abort</b>
       *   if there was only whitespace until the end-of-file.
       **/
       public char charRead()
       {
          int next = pastWhitespace();
          if (next == EOF)
              badExit("expected char, not EOF!");
          return (char) next;
       }
     
       /**
       * Skip whitespace, read a character, and skip the rest of the line.
       * @param none
       * @return
       *   a character read that is not whitespace 
       * 
       * <dt><b>Abort</b>
       *   if there was only whitespace until the end-of-file.
       **/
       public char charReadln()
       {
          char answer = charRead();
          skipLine();
          return answer;
       }
     
     
       /**
       * Read a <CODE>double</CODE> number.
       * @param none
       * @return
       *   a </CODE>double</CODE> number that's been read
       * <dt><b>Parsing details:</b>  Skip whitespace and then read 
       *   an optional sign, 0 or more digits, an optional '.' 
       *   and 0 or more digits, and an optional exponent 
       *   including an 'e' or 'E', optional sign, and digits.  
       *   The first character not fitting this pattern is left in the input stream.
       *   The extracted string is converted using <CODE>Double.valueOf</CODE>.
       * <dt><b>Abort</b>
       *   if the conversion to a double causes an exception.
       **/
       public double doubleRead()
       {
          StringBuffer input = new StringBuffer();
     
          StringBuffer buffer = new StringBuffer(80);
          int next = findInteger(pastWhitespace(), buffer);
          if (next == '.')
          {  // Read the decimal point and fractional part.
             buffer.append('.');
             next = findDigits(read(), buffer);
          }
          if (next == 'E' || next == 'e')
          {  
             buffer.append('E');
             next = findInteger(read(), buffer);
          }
          unread(next);  // only private methods end up reading ahead
          double d = 0.0;
          try
          {
             d = Double.valueOf(buffer.toString()).doubleValue();
          }
          catch (NumberFormatException e)
          {
             badExit("bad double format!");
          }
          return d;
       }
     
     
       /**
       * Read a double value from a complete line.
       * @param none
       * @return
       *   a double value that's been read
       * <dt><b>Note:</b><dd>
       *   This method is identical to <CODE>doubleRead()</CODE> with 
       *   <CODE>skipLine()</CODE> called just before returning.
       **/
       public double doubleReadln()
       {
          double answer = doubleRead();
          skipLine();
          return answer;
       }
     
     
       /**
       * Read an int value.
       * @param none
       * @return
       *   an int that has been read
       * <dt><b>Parsing details:</b>  Skip whitespace and then read 
       *   an optional sign and digits.  
       *   The first character not fitting this pattern is left in the input stream.
       *   The extracted string is converted using <CODE>Integer.parseInt</CODE>.
       * <dt><b>Abort</b>
       *   if the conversion to an int causes an exception.
       **/
       public int intRead()
       {
          StringBuffer buffer = new StringBuffer(80);
          unread(findInteger(pastWhitespace(), buffer));
          int i = 0;
          try
          {
             i = Integer.parseInt(buffer.toString());
          }
          catch (NumberFormatException e)
          {
             badExit("bad int format!");
          }
          return i;
       }
     
     
       /**
       * Read an int value from a complete line.
       * @param none
       * @return
       *   an int that has been read
       * <dt><b>Note:</b><dd>
       *   This method is identical to <CODE>intRead()</CODE> with
       *   <CODE>skipLine()</CODE> called just before returning.
       **/
       public int intReadln()
       {
          int answer = intRead();
          skipLine();
          return answer;
       }
     
     
       /**
       * Read a long value.
       * @param none
       * @return
       *   a long value that has been read
       * <dt><b>Parsing details:</b>  Skip whitespace and then read 
       *   an optional sign and digits.  
       *   The first character not fitting this pattern is left in the input stream.
       *   The extracted string is converted using <CODE>Long.parseInt</CODE>.
       * <dt><b>Abort</b>
       *   if the conversion to a long causes an exception.
       **/
       public long longRead()
       {
          StringBuffer buffer = new StringBuffer(80);
          unread(findInteger(pastWhitespace(), buffer));
          long l = 0;
          try
          {
             l = Long.parseLong(buffer.toString());
          }
          catch (NumberFormatException e)
          {
             badExit("bad long format!");
          }
          return l;
       }
     
       /**
       * Read a long value from a complete line.
       * @param none
       * @return
       *   a long value that has been read
       * <dt><b>Note:</b><dd>
       *   This method is identical to <CODE>longRead()</CODE> with
       *   <CODE>skipLine()</CODE> called just before returning.
       **/
       public long longReadln()
       {
          long answer = longRead();
          skipLine();
          return answer;
       }
     
       /**
       * Read the next whitespace-delimited <CODE>String</CODE>.
       * @param none
       * @return
       *   a <CODE>String</CODE> of non-whitespace characters read
       * <dt><b>Abort</b>
       *   if there are no non-whitespace characters until end-of-file.
       **/
       public String stringRead()
       {
          StringBuffer buffer = new StringBuffer(80);
     
          int next = pastWhitespace();
          while (next != EOF && !Character.isWhitespace((char)next)) {
             buffer.append((char)next);
             next = read();
          }
          unread(next);
          if (buffer.length() == 0)
              badExit("expected non-whitespace char, not EOF!");
          return buffer.toString();
       }
     
       /**
       * Read the next whitespace-delimited <CODE>String</CODE>,
       * and skip the rest of the line it is on.
       * @param none
       * @return
       *   a <CODE>String</CODE> of non-whitespace characters read
       * <dt><b>Abort</b>
       *   if there are no non-whitespace characters until end-of-file.
       **/
       public String stringReadln()
       {
          String s = stringRead();
          skipLine();
          return s;
       }
     
     
       /**
       * Determine whether the last character of the file has been read.
       * @param none
       * @return
       *   true if all characters up to but not necessaily including EOF have been read.
       **/
       public boolean isEOF()
       {
          int next = read();
          unread(next);
          return next == EOF;
       }
     
     
       /**
       * Determine whether the next input character is an end of line or end of file.
       * @param none
       * @return
       *   true if the next input character is '\n' or '\r', 
       *   or if <CODE>isEOF()</CODE>
       **/
       public boolean isEOLN()
       {
          int next = read();
          unread(next);
          return (next == '\n') || (next == '\r' || next == EOF);
       }
     
    //private input methods ------------------------------------------------------------
       private static void badExit(String errMsg) {  
          // abort with message
          System.err.println("Aborting -- " + errMsg);
          System.exit(-1);
       }
     
       private int read()
       // Read and return the next character or abort.
       {
          int next = 0;
          try
          {
             next = in.read();
          }
          catch (IOException e)
          {
             badExit("File reading error!  " + e.getMessage());
          }
          return next;
       }
     
       private void unread(int next)
       // Unread next or abort.
       {
          if (next == EOF) return;
          try
          {
             in.unread(next);
          }
          catch (IOException e)
          {
             badExit("File unreading error!  " + e);
          }
       }
     
       private int findDigits(int next, StringBuffer buffer)
       // append any digits, starting with next to buffer, return first non-digit
       {
          while (Character.isDigit((char)next)) {
             buffer.append((char)next);
             next = read();
          }
          return next;
       }
     
       private int findInteger(int next, StringBuffer buffer)
       // append any sign+digits, starting with next to buffer, 
       //   return first char not matching pattern
       {
          if (next == '+') next = read();
          if (next == '-') {
            buffer.append('-');
            next = read();
          }
          return findDigits(next, buffer);
       }
     
       private int pastWhitespace()
       // Read and discard whitespace: blanks, tabs, and newlines.
       // return first non-whitespace char
       {
          int next = read();
          while (Character.isWhitespace((char)next))
             next = read();
          return next;   
       }
     
     
    // String formatting routines ----------------------------------------------
     
       private static NumberFormat numForm = NumberFormat.getNumberInstance();
     
       /**
       * Format a double as a </CODE>String</CODE> with a specified format.
       * @param <CODE>d</CODE>
       *   the number to be printed
       * @param <CODE>minimumWidth</CODE>
       *   |minimumWidth| is the minimum number of characters in the entire 
       *   output, and the positive or negative sign indicates right or left 
       *   justification.
       * @param <CODE>fractionDigits</CODE>
       *   the number of digits to print, with rounding, on the right side 
       *   of the decimal point.  A negatine fractionDigits is treated like 0.
       *   No decimal point is printed if <CODE>fractionDigits</CODE> is 0.
       **/
       public static String format(double d, int minimumWidth, int fractionDigits)
       {
          numForm.setGroupingUsed(false);  // no commas
          if (fractionDigits < 0) fractionDigits = 0;
          numForm.setMinimumFractionDigits(fractionDigits);
          numForm.setMaximumFractionDigits(fractionDigits);
          return format(numForm.format(d), minimumWidth);
       }
     
       /**
       * Create a left or right justified String from an <CODE>Object</CODE> .
       * @param <CODE>o</CODE>
       *   the Object to be printed
       * @param <CODE>minimumWidth</CODE>
       *   |minimumWidth| is the minimum number of characters in the entire 
       *   output, and the positive or negative sign indicates right or left 
       *   justification.
       **/
       public static String format(Object o, int minimumWidth)
       {
          String s = o.toString();
          int nBlanks = Math.abs(minimumWidth) - s.length();
          if (nBlanks <= 0) return s;
          String BLANK40 = "                                        ";
          String pad = "";
          while (nBlanks >= 40) {
              pad += BLANK40;
              nBlanks -= 40;
          }
          pad += BLANK40.substring(0,nBlanks);
          if (minimumWidth > 0)
            return pad+s;
          else
            return s+pad;
       }
     
       /**
       * Create a left or right justified String from a double value.
       * @param <CODE>d</CODE>
       *   the number to be printed
       * @param <CODE>minimumWidth</CODE>
       *   |minimumWidth| is the minimum number of characters in the entire 
       *   output, and the positive or negative sign indicates right or left 
       *   justification.
       **/
       public static String format(double d, int minimumWidth)
       {
          return format(""+ d, minimumWidth);
       }
     
       /**
       * Create a left or right justified String from a long value.
       * @param <CODE>n</CODE>
       *   the number to be printed
       * @param <CODE>minimumWidth</CODE>
       *   |minimumWidth| is the minimum number of characters in the entire 
       *   output, and the positive or negative sign indicates right or left 
       *   justification.
       **/
       public static String format(long n, int minimumWidth)
       {
          return format("" + n, minimumWidth);
       }
     
    }

    any help would be very much appreciated.
    Last edited by madkris; March 15th, 2009 at 01:12 PM.

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] merging java classes

    Yes you can put both classes into 1 file. Please take a look at this tutorial:

    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    It will also be good to read through:

    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    Are you looking to have the 'autoedit' class and the 'ACMIO' class in the same file?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SOLVED] merging java classes

    Quote Originally Posted by JavaPF View Post
    Are you looking to have the 'autoedit' class and the 'ACMIO' class in the same file?
    Yes, I am. Reading about nested classes atm. Will try to search google for more info about it.

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] merging java classes

    Hey madkris,

    Here is 'autoedit' & 'ACMIO' together.

    import java.io.*;
    import java.io.FileReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.PushbackReader;
    import java.text.NumberFormat;
     
    class autoedit {
     
        public static void main(String[] arg) {
            String[] find = new String[10];
            String[] replace = new String[10];
            ACMIO in = new ACMIO("autoedit.in");
            PrintWriter out = null;
            try {
                out = new PrintWriter(new BufferedWriter(new FileWriter(
                        "autoedit.out")));
            } catch (Exception e) {
                System.out.println("can't open output");
            }
            int nrep = in.intReadln();
            while (nrep > 0) {
                for (int i = 0; i < nrep; i++) {
                    find[i] = in.getLine();
                    replace[i] = in.getLine();
                }
                String line = in.getLine();
                for (int i = 0; i < nrep; i++) {
                    int pos = line.indexOf(find[i]);
                    while (pos >= 0) {
                        line = line.substring(0, pos) + replace[i]
                                + line.substring(pos + find[i].length());
                        pos = line.indexOf(find[i]);
                    }
                }
                out.println(line);
                nrep = in.intReadln();
            }
            out.close();
        }
    }
     
    class ACMIO {
        private static ACMIO stdin = null;
        private PushbackReader in;
     
        final static int EOF = -1; // Returned by in.read() at EOF
     
        /**
         * Construct an ACMIO to read from a file.
         * 
         * @param <code>filename</code> file to read from. <dt><b>Abort</b> if the
         *        file cannot be opened to read.
         **/
        public ACMIO(String filename) {
            try {
                in = new PushbackReader(new FileReader(filename));
            } catch (Exception e) {
                badExit("can't open file " + filename);
            }
        }
     
        private ACMIO() {
            in = new PushbackReader(new InputStreamReader(System.in));
        }
     
        /**
         * Returns the <code>ACMIO</code> object that reads from
         * <code>System.in</code>.
         * 
         * @param none
         * @return <code>ACMIO</code> object that reads from <code>System.in</code>
         **/
        public static ACMIO getStdin() {
            if (stdin == null)
                stdin = new ACMIO();
            return stdin;
        }
     
        /**
         * Read and discard whitespace: blanks, tabs, and newlines.
         * 
         * @param none
         **/
        public void skipWhitespace() {
            unread(pastWhitespace());
        }
     
        /**
         * Read and discard blanks.
         * 
         * @param none
         **/
        public void skipBlanks() {
            int next = read();
            while (' ' == next)
                next = read();
            unread(next);
        }
     
        /**
         * Read and discard the rest of the current input line.
         * 
         * @param none
         **/
        public void skipLine() {
            int next = read();
            while (next != '\n' && next != EOF)
                next = read();
        }
     
        /**
         * Read until the end of the current line.
         * 
         * @param none
         * @return a <CODE>String</CODE> with all characters other that '\r' or '\n'
         *         until the end of the line, or end-of-file if that comes first.
         **/
        public String getLine() {
            StringBuffer buffer = new StringBuffer(80);
            int next = read();
            while (next != '\n' && next != EOF) {
                if (next != '\r')
                    buffer.append((char) next);
                next = read();
            }
            return buffer.toString();
        }
     
        /**
         * Read a character.
         * 
         * @param none
         * @return a character read
         * 
         *         <dt><b>Abort</b> if tried to read at end-of-file.
         **/
        public char getChar() {
            int next = read();
            if (next == EOF)
                badExit("expected char, not EOF!");
            return (char) next;
        }
     
        /**
         * Skip whitespace and read a character.
         * 
         * @param none
         * @return a character read that is not whitespace
         * 
         *         <dt><b>Abort</b> if there was only whitespace until the
         *         end-of-file.
         **/
        public char charRead() {
            int next = pastWhitespace();
            if (next == EOF)
                badExit("expected char, not EOF!");
            return (char) next;
        }
     
        /**
         * Skip whitespace, read a character, and skip the rest of the line.
         * 
         * @param none
         * @return a character read that is not whitespace
         * 
         *         <dt><b>Abort</b> if there was only whitespace until the
         *         end-of-file.
         **/
        public char charReadln() {
            char answer = charRead();
            skipLine();
            return answer;
        }
     
        /**
         * Read a <CODE>double</CODE> number.
         * 
         * @param none
         * @return a </CODE>double</CODE> number that's been read <dt><b>Parsing
         *         details:</b> Skip whitespace and then read an optional sign, 0 or
         *         more digits, an optional '.' and 0 or more digits, and an
         *         optional exponent including an 'e' or 'E', optional sign, and
         *         digits. The first character not fitting this pattern is left in
         *         the input stream. The extracted string is converted using
         *         <CODE>Double.valueOf</CODE>. <dt><b>Abort</b> if the conversion
         *         to a double causes an exception.
         **/
        public double doubleRead() {
            StringBuffer input = new StringBuffer();
     
            StringBuffer buffer = new StringBuffer(80);
            int next = findInteger(pastWhitespace(), buffer);
            if (next == '.') { // Read the decimal point and fractional part.
                buffer.append('.');
                next = findDigits(read(), buffer);
            }
            if (next == 'E' || next == 'e') {
                buffer.append('E');
                next = findInteger(read(), buffer);
            }
            unread(next); // only private methods end up reading ahead
            double d = 0.0;
            try {
                d = Double.valueOf(buffer.toString()).doubleValue();
            } catch (NumberFormatException e) {
                badExit("bad double format!");
            }
            return d;
        }
     
        /**
         * Read a double value from a complete line.
         * 
         * @param none
         * @return a double value that's been read <dt><b>Note:</b>
         *         <dd>
         *         This method is identical to <CODE>doubleRead()</CODE> with
         *         <CODE>skipLine()</CODE> called just before returning.
         **/
        public double doubleReadln() {
            double answer = doubleRead();
            skipLine();
            return answer;
        }
     
        /**
         * Read an int value.
         * 
         * @param none
         * @return an int that has been read <dt><b>Parsing details:</b> Skip
         *         whitespace and then read an optional sign and digits. The first
         *         character not fitting this pattern is left in the input stream.
         *         The extracted string is converted using
         *         <CODE>Integer.parseInt</CODE>. <dt><b>Abort</b> if the conversion
         *         to an int causes an exception.
         **/
        public int intRead() {
            StringBuffer buffer = new StringBuffer(80);
            unread(findInteger(pastWhitespace(), buffer));
            int i = 0;
            try {
                i = Integer.parseInt(buffer.toString());
            } catch (NumberFormatException e) {
                badExit("bad int format!");
            }
            return i;
        }
     
        /**
         * Read an int value from a complete line.
         * 
         * @param none
         * @return an int that has been read <dt><b>Note:</b>
         *         <dd>
         *         This method is identical to <CODE>intRead()</CODE> with
         *         <CODE>skipLine()</CODE> called just before returning.
         **/
        public int intReadln() {
            int answer = intRead();
            skipLine();
            return answer;
        }
     
        /**
         * Read a long value.
         * 
         * @param none
         * @return a long value that has been read <dt><b>Parsing details:</b> Skip
         *         whitespace and then read an optional sign and digits. The first
         *         character not fitting this pattern is left in the input stream.
         *         The extracted string is converted using
         *         <CODE>Long.parseInt</CODE>. <dt><b>Abort</b> if the conversion to
         *         a long causes an exception.
         **/
        public long longRead() {
            StringBuffer buffer = new StringBuffer(80);
            unread(findInteger(pastWhitespace(), buffer));
            long l = 0;
            try {
                l = Long.parseLong(buffer.toString());
            } catch (NumberFormatException e) {
                badExit("bad long format!");
            }
            return l;
        }
     
        /**
         * Read a long value from a complete line.
         * 
         * @param none
         * @return a long value that has been read <dt><b>Note:</b>
         *         <dd>
         *         This method is identical to <CODE>longRead()</CODE> with
         *         <CODE>skipLine()</CODE> called just before returning.
         **/
        public long longReadln() {
            long answer = longRead();
            skipLine();
            return answer;
        }
     
        /**
         * Read the next whitespace-delimited <CODE>String</CODE>.
         * 
         * @param none
         * @return a <CODE>String</CODE> of non-whitespace characters read <dt>
         *         <b>Abort</b> if there are no non-whitespace characters until
         *         end-of-file.
         **/
        public String stringRead() {
            StringBuffer buffer = new StringBuffer(80);
     
            int next = pastWhitespace();
            while (next != EOF && !Character.isWhitespace((char) next)) {
                buffer.append((char) next);
                next = read();
            }
            unread(next);
            if (buffer.length() == 0)
                badExit("expected non-whitespace char, not EOF!");
            return buffer.toString();
        }
     
        /**
         * Read the next whitespace-delimited <CODE>String</CODE>, and skip the rest
         * of the line it is on.
         * 
         * @param none
         * @return a <CODE>String</CODE> of non-whitespace characters read <dt>
         *         <b>Abort</b> if there are no non-whitespace characters until
         *         end-of-file.
         **/
        public String stringReadln() {
            String s = stringRead();
            skipLine();
            return s;
        }
     
        /**
         * Determine whether the last character of the file has been read.
         * 
         * @param none
         * @return true if all characters up to but not necessaily including EOF
         *         have been read.
         **/
        public boolean isEOF() {
            int next = read();
            unread(next);
            return next == EOF;
        }
     
        /**
         * Determine whether the next input character is an end of line or end of
         * file.
         * 
         * @param none
         * @return true if the next input character is '\n' or '\r', or if
         *         <CODE>isEOF()</CODE>
         **/
        public boolean isEOLN() {
            int next = read();
            unread(next);
            return (next == '\n') || (next == '\r' || next == EOF);
        }
     
        // private input methods
        // ------------------------------------------------------------
        private static void badExit(String errMsg) {
            // abort with message
            System.err.println("Aborting -- " + errMsg);
            System.exit(-1);
        }
     
        private int read()
        // Read and return the next character or abort.
        {
            int next = 0;
            try {
                next = in.read();
            } catch (IOException e) {
                badExit("File reading error!  " + e.getMessage());
            }
            return next;
        }
     
        private void unread(int next)
        // Unread next or abort.
        {
            if (next == EOF)
                return;
            try {
                in.unread(next);
            } catch (IOException e) {
                badExit("File unreading error!  " + e);
            }
        }
     
        private int findDigits(int next, StringBuffer buffer)
        // append any digits, starting with next to buffer, return first non-digit
        {
            while (Character.isDigit((char) next)) {
                buffer.append((char) next);
                next = read();
            }
            return next;
        }
     
        private int findInteger(int next, StringBuffer buffer)
        // append any sign+digits, starting with next to buffer,
        // return first char not matching pattern
        {
            if (next == '+')
                next = read();
            if (next == '-') {
                buffer.append('-');
                next = read();
            }
            return findDigits(next, buffer);
        }
     
        private int pastWhitespace()
        // Read and discard whitespace: blanks, tabs, and newlines.
        // return first non-whitespace char
        {
            int next = read();
            while (Character.isWhitespace((char) next))
                next = read();
            return next;
        }
     
        // String formatting routines ----------------------------------------------
     
        private static NumberFormat numForm = NumberFormat.getNumberInstance();
     
        /**
         * Format a double as a </CODE>String</CODE> with a specified format.
         * 
         * @param <CODE>d</CODE> the number to be printed
         * @param <CODE>minimumWidth</CODE> |minimumWidth| is the minimum number of
         *        characters in the entire output, and the positive or negative sign
         *        indicates right or left justification.
         * @param <CODE>fractionDigits</CODE> the number of digits to print, with
         *        rounding, on the right side of the decimal point. A negatine
         *        fractionDigits is treated like 0. No decimal point is printed if
         *        <CODE>fractionDigits</CODE> is 0.
         **/
        public static String format(double d, int minimumWidth, int fractionDigits) {
            numForm.setGroupingUsed(false); // no commas
            if (fractionDigits < 0)
                fractionDigits = 0;
            numForm.setMinimumFractionDigits(fractionDigits);
            numForm.setMaximumFractionDigits(fractionDigits);
            return format(numForm.format(d), minimumWidth);
        }
     
        /**
         * Create a left or right justified String from an <CODE>Object</CODE> .
         * 
         * @param <CODE>o</CODE> the Object to be printed
         * @param <CODE>minimumWidth</CODE> |minimumWidth| is the minimum number of
         *        characters in the entire output, and the positive or negative sign
         *        indicates right or left justification.
         **/
        public static String format(Object o, int minimumWidth) {
            String s = o.toString();
            int nBlanks = Math.abs(minimumWidth) - s.length();
            if (nBlanks <= 0)
                return s;
            String BLANK40 = "                                        ";
            String pad = "";
            while (nBlanks >= 40) {
                pad += BLANK40;
                nBlanks -= 40;
            }
            pad += BLANK40.substring(0, nBlanks);
            if (minimumWidth > 0)
                return pad + s;
            else
                return s + pad;
        }
     
        /**
         * Create a left or right justified String from a double value.
         * 
         * @param <CODE>d</CODE> the number to be printed
         * @param <CODE>minimumWidth</CODE> |minimumWidth| is the minimum number of
         *        characters in the entire output, and the positive or negative sign
         *        indicates right or left justification.
         **/
        public static String format(double d, int minimumWidth) {
            return format("" + d, minimumWidth);
        }
     
        /**
         * Create a left or right justified String from a long value.
         * 
         * @param <CODE>n</CODE> the number to be printed
         * @param <CODE>minimumWidth</CODE> |minimumWidth| is the minimum number of
         *        characters in the entire output, and the positive or negative sign
         *        indicates right or left justification.
         **/
        public static String format(long n, int minimumWidth) {
            return format("" + n, minimumWidth);
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. #11
    Junior Member
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SOLVED] merging java classes

    I swear I tried doing that last night.. I must have missed something. Will try to see where I went wrong since I still have a copy of what I did last night.

    thank you very much.

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] merging java classes

    No problem.

    All I basically did was to bring the imports for both classes to the top and inserted the 'ACMIO' class below the end of the 'autoedit' class.

    Glad I could help.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM
  2. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM
  3. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM
  4. Replies: 1
    Last Post: March 11th, 2009, 04:41 PM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM