The Artima Developer Community
Sponsored Link

Java Answers Forum
Help! how to construct program to run in dos

6 replies on 1 page. Most recent reply: Feb 10, 2003 12:51 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Help! how to construct program to run in dos Posted: Feb 6, 2003 10:25 AM
Reply to this message Reply
Advertisement
i need something like this as output in my dos program. Anyone can help?


000000000000000
11111111111111
2222222222222
333333333333
44444444444
5555555555
666666666
77777777
8888888
999999
00000
1111
222
33
4


What's more, i was told tt this program can run for int up to 100! How to solve this?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help! how to construct program to run in dos Posted: Feb 6, 2003 11:18 AM
Reply to this message Reply
I don't think there is a JVM for DOS, so you'll have to write it in C, C++, Pascal, Python (there is a Python interpreter for DOS), etc. Actually, you could practically write it with a batch file...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help! how to construct program to run in dos Posted: Feb 6, 2003 11:23 AM
Reply to this message Reply
Here it is in Python by the way:
def Nummy(n):
   for i in range(n):
      print ' '*i + str(i)[-1]*(n-i)

Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Re: Help! how to construct program to run in dos Posted: Feb 6, 2003 12:05 PM
Reply to this message Reply
I tried my own method but juz can't get something like this. My output does not have all the spacing...it looks like this

000000000000000
11111111111111
2222222222222
etc...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help! how to construct program to run in dos Posted: Feb 6, 2003 2:43 PM
Reply to this message Reply
/**
 * Nummy.java
 *
 * Simple demo of printing meaningless rows of numbers.
 *
 * Copyright 2003, Matt Gerrans.
 *
 * Turning in this code as homework is a violation of ethical principals,
 * and punishable by severe penalties, even if you change the variable
 * names.
 */
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
 
public class Nummy 
{
   public static List getHomeworkAssignmentLinesOfText(int howMany)
   {
      List lines = new LinkedList();
      StringBuffer line = new StringBuffer();
      for( int i = 0; i < howMany; i++ )
      {
         for( int j = 0; j < howMany; j++ )
            line.append( j < i ? " " : Integer.toString(i%10) );
 
         lines.add(line.toString());
         line.setLength(0);
      }
      return lines;
   }
 
   public static void main(String args[])
   {
      int n = 10;
      if( args.length > 0 )
         try
         {
            n = Integer.parseInt(args[0]);
         }
         catch( NumberFormatException nfe )
         {
            String msg = 
              nfe.getMessage() + ", integer conversion is not impossible!\n" +
              "You should specify the number of lines you'd like to see as\n" +
              "a plain old integer value, like 100.\n" +
              "Here's the results with " + n + ":\n";
            
            System.out.println( msg );
         }
 
      Iterator lineIterator = getHomeworkAssignmentLinesOfText(n).iterator();
      while( lineIterator.hasNext() )
         System.out.println( lineIterator.next() );
   }
}

Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Re: Help! how to construct program to run in dos Posted: Feb 9, 2003 6:05 AM
Reply to this message Reply
I dun really get this method cos i am really new at java. I was told that this thing can be done by a quite simple method and that's what i have been trying to do. I tried using rows and columns theory and i've tried simple integer stuff, but to no avail. Pls help.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help! how to construct program to run in dos Posted: Feb 10, 2003 12:51 PM
Reply to this message Reply
I don't know what you mean about rows and columns, but how's this?

/**
 * Nummy.java
 *
 * Simple demo of printing meaningless rows of numbers.
 *
 * Copyright 2003, Matt Gerrans.
 *
 * Turning in this code as homework is a violation of ethical principals,
 * and punishable by severe penalties, even if you change the variable
 * names.
 */
public class Nummy 
{
   /** 
    * Gets the number of colums (and lines) to display from the command
    * line.   Returns 10, by default (in case of error, or no parameters).
    *
    * (You can ignore this part, if you are getting the number of columns
    *  some other way.)
    */
   public static int getColumnCountFromCommandLine( String args[] )
   {
      int n = 10;
      if( args.length > 0 )
         try
         {
            n = Integer.parseInt(args[0]);
         }
         catch( NumberFormatException nfe )
         {
            String msg = 
              nfe.getMessage() + ", integer conversion is not impossible!\n" +
              "You should specify the number of lines you'd like to see as\n" +
              "a plain old integer value, like 100.\n" +
              "Here's the results with " + n + ":\n";
            
            System.err.println( msg );
         }
      return n;
   }
 
   /**
    * Simple method to print out a silly bunch of lines.
    * This one more closely mimics this Python code:
    * <pre>
    %
    *    for i in range(howManyColumns):
    *       print ' '*i + str(i)[-1]*(howManyColumns-i)
    *
    * </pre>
    *
    * Note that it is probably a bad idea to have this method write
    * directly to the screen, but this is after all, a very simple 
    * method which demonstrates for-loops, if not good program design.
    */
   public static void simpleMethod( int howManyColumns )
   {
      for( int i = 0; i < howManyColumns; i++ )
      {
         for( int j = 0; j < i; j++ )
            System.out.print( " " );
 
         // "Integer.toString(i%10)" gives you the single-character string for the last digit of i.
         for( int j = i; j < howManyColumns; j++ )
            System.out.print( Integer.toString(i%10) );
 
         System.out.println();
      }
   }
 
   public static void main(String args[])
   {
      int howManyColumns = getColumnCountFromCommandLine(args);
 
      simpleMethod(howManyColumns);
   }
}

Flat View: This topic has 6 replies on 1 page
Topic: help to resolve errors Previous Topic   Next Topic Topic: yahoo games

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use