Creating a basic loop program in java

I'm working on a program for school. The program asks the user for an in put number between 1-20. The program then outputs a square made of asterisks that corresponds with the number entered. for example :
2 =
3 =
This is what I have so far, but I don't know how to create a straight line of asterisks for an input number of 3 or higher
public class square {
* @param args the command line arguments
public static void main(String[] args)
Scanner input = new Scanner( System.in);
int number = 0;
System.out.print( "Enter a number between 1 and 20\t" );
number = input.nextInt();
if (number <1)
System.out.printf( " Number must be greater than 0 ");
if (number >20)
System.out.printf(" Number must not be greater than 20");
if (number ==1)
System.out.printf("*");
if (number ==2)
System.out.println ("**\n**");
if (number >= 3)
Any help would be greatly appreciated!
Edited by: Methodx on Jan 22, 2009 9:24 PM

You would need two for loops .
In the inner loop , print either an asterix or a space based on wheter the point ur considering is on the edge of the square or inside the square.
In the outer loop , skip to the next line.
(0,0)    (0,1)    (0,2)     (0,3)
(1,0)    (1,1)    (1,2)     (1,3)
(2,0)    (2,1)    (2,2)     (2,3)
(3,0)    (3,1)    (3,2)     (3,3)You can work out the condition to be used in the inner loop ( to wheter print an asterix or a space) by studying the values of the loop variables at the edge of the square.

Similar Messages

  • Can i create an exetutable (.exe) program with java???????

    I would like to learn how i can create an exetutable (.exe) program with java.
    Is there any additional package which i should download or buy it?
    Also i would like to write files into CD's direct from my application.
    Is there any library which i must download or buy?
    Thanks!

    check out the free open source GNU gcj native compiler for Java to see if it will do what you want.
    http://gcc.gnu.org/java/
    for writing data files you could use java.io.BufferedWriter. For sound files, look in the javax.sound package

  • Creating a drawing (CAD) program in Java

    I'm in the planning stages of creating a drawing/drafting program in Java, and I'm fairly new to Swing, so I need some guidance.
    For a drafting program, I will need to be able to draw all kinds of objects using lines, curves, etc, connected in various ways. One thing I could do is extend JComponent and override the paint() method, and have it put up all the various objects as it repaints. The problem with this is that if a user clicks on an object, how will I detect which object he clicked on? I'll get mouse coordinates, but then how do I go from that to knowing that he clicked on a certain curve, etc?
    The alternative is to not extend JComponent, but rather to make each of the individual objects on the screen its own component and then lay them out using absolute positioning. Then I'll get mouse events as the user clicks on things. Is this a better way to do it? And if so, I guess I'll still need to create individual components that represent lines, arcs, curves, etc?
    Swing has so many features that it's hard for a beginning to get started into it. Thanks for any tips.

    Personally I wouldn't work with Swing for this functionality,
    but I would rather work with java 2D instead.
    Swing contains to much overhead for the kind of application you plan to develop.
    Look for the java.awt.Shape interface and to the java.awt.geom.* implementations of such shapes.
    It contains already most of the functionality required for painting and hit-detection of a shape on the
    screen.
    kind regards,

  • Problems to create a basic LED program

    Hi guys, I'm starting to work with Labview and also Arduino and I'm facing some problems. I created a program to make two LEDs blink in parallel using Arduino. This program worked fine, but now I want to make it a little bit more complex inserting a button to select which one of the LEDs will blink. I created a logic to do this but it's not possible to implement it due to the different kind of connections (digital, boolean, string). I would like to know if someone can help me with this. I'm posting the VI. Note that I left the logic that I created in the VI, so you can see what I thought to do. Thank you!
    *Ps: I don't know why I'm not able to post my VI. When I try to post with it attached I receive the following message: "The contents of the attachment doesn't match its file type.". So i'm posting one picture of the circuit. =/

    What options are you looking for?  Both blink, neither blink, LED1 only blinks, LED2 only blinks?
    As far as not able to post a VI, we've had that issue for awhile.  I just zip up any VIs I need to post and post the zip file.  Snippets also work well.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How to create a directory or Folder  using java program?

    Hi all,
    Can any one know, how to create a directory(new folder) in java.
    can any give me some idea, on creating a directory using java program dynamically.
    thanx in advance

    hi thanks for your answer,
    sorry, actual i know this technique(its my fault i didnt mentioned it)
    i am looking for some what different technique.
    bye
    ram

  • How to create a  schedule program in java

    Hello Friends ,
    Can any one provide me with an example how to create a schedule program using java.util.timer etc . I am in need of a program which should run as a schedule job that searches for a file in a directory and read the latest files from that directory in every minute of time as a schedule job .
    Thanks
    mahesh

    I don't feel like writing my own example, but google will be happy to provide you with an example.

  • Is it possible to create java standalone programs with java ME 8

    Hi,
    Comparing java ME to java SE, I have the impression that midlets are similar to applets.
    When working with java SE, I usually make standalone programs.
    Now, I wonder if it is also possible to create also standalone programs for java ME, if the classes are only  using java ME API's.
    I already noticed that netbeans does not allow to build a project that does not contain a midlet, but maybe, it is possible to create a jar file
    using the javac and jar commands. Or is there a specific reason why netbeans does not allow to build a project without midlets?
    The reason for asking is, that I often make small tools in java that ae then called from a linux script. I imagine that this might be useful on the raspberry pi as well.
    Thanks in advance
    Jef

    Hi Jef -
    Comparing java ME to java SE, I have the impression that midlets are similar to applets.
    Sort of - similar life cycle methods - so conceptually, yes.
    Now, I wonder if it is also possible to create also standalone programs for java ME, if the classes are only  using java ME API's.
    In a way, yes. If you build an project, you will notice a JAD file and JAR file in the dist directory of the project.  JAD is the descriptor for a MIDlet - in mobile devices, the JAD
    file is loaded first to determine if the MIDlet has enough privileges to load and run, before the corresponding JAR file is downloaded.
    However, the JAD file can also be included in the Manifest of the JAR and the JAR copied to the embedded device manually and run there.
    I already noticed that netbeans does not allow to build a project that does not contain a midlet, but maybe, it is possible to create a jar file
    using the javac and jar commands. Or is there a specific reason why netbeans does not allow to build a project without midlets?
    You can build JAR files and include them into Java ME Embedded projects, but just like Applets, to use the Application Management System (AMS) on the Raspberry Pi, you must have a MIDlet to run.
    Perhaps you mean that you would like to launch the AMS and load a MIDlet from the command line on the Pi?
    If so, take a look at this section of the Getting Started Guide for the Raspberry Pi.
    Tom

  • How to creat a Dialer Program in Java

    Help me in creating a Dialer program which dials to a specifie ISP.It should work like the Dialer provided in windows.Help!! Can we code such a program in JAVA.If yes, HOW!!!!
    Abhishek([email protected])

    Get the Java Communications package - javax.comm (comm.jar)
    This will give you access to Windows/Solaris serial (and parallel) ports from Java.
    You then need some simple modem handling to perform the dial and initial handshake.
    Steve

  • Can we create our own loops? Basic question.

    Hi,
    Can we create our own loops in Logic Pro. I see in all the tutorials. They are either taking input from external instruments OR use existing apple loops.
    I have tried FL studio demo to create our own loops. But there is no trial version for Logic Pro. Need your inputs before I purchase it.
    Thank you.
    Anji

    Yes you can!
    It is as simple as selecting a Region (optional trimming recommended) and choose the Export command.
    A Dialog window opens up that lets you enter the metadata
    Apple had its separate "Apple Loop Utility" app (now only available in the developer package) that allowed you to create loops in a standalone app. The built-in loop capability in Logic uses part of that engine.
    Hope that helps
    Edgar Rothermich
    http://DingDingMusic.com/Manuals/
    'I may receive some form of compensation, financial or otherwise, from my recommendation or link.'

  • What is url and how it should be created thru program in java

    Respected people,
    could You please tell me what is a URL in java ?
    and
    How the Url is programmed in java
    Regards,
    Kamakshigopal

    Respected people,
    could You please tell me what is a URL in java ?
    and
    How the Url is programmed in java
    Regards,
    Kamakshigopal

  • How to create an infinite loop that cannot be cancelled?

    Hi , im new around java and i need a program that keeps on repeating after each each input/uses(generally meant for different user) and cannot either stop using "Cancel" or "x" while stopping only using the "stop" button
    I got a few lines of codes that look like this : String arg = "";
    do{
    JOptionPane.showInputDialog("hi");
    }while ( arg != "y" || arg!= "Y");
    and it cannot be stop with both cancel and"x"(which i wanted)
    but when i used this logic with
    Valid validUserInput = new Valid();
    do {
    userInput = validUserInput.validUserInput(a);
    switch(userInput){
    case 1 : validUserInput.optionOne(b);break;
    case 2 : validUserInput.optionTwo(c);break;
    case 3 : validUserInput.optionThree(d,e);break;
    default : validUserInput.validUserInput(f);
    }while(looping != "" || looping != " ");
    it does not really work(while it will keep looping, it can be easily stop by pressing cancelled or "x")
    So is there any logic or method that can prevent the program from being stop?
    Thankyou!

    elricscript wrote:
    "That is helpful. It gives him a direction where he can focus his research. Namely, 'how to run a program in kiosk mode' rather than 'how to create an infinite loop that cannot be cancelled.'"
    No... He already mentioned he wants to "e.g Interactive kiosk", so saying "properly kiosk it" isn't adding any new information. Rather it's just giving the person a dead point of: you're doing it wrong. Pointing out someone is wrong is not a positive enlightenment. Pointing someone in the right direction is more productive.
    >"That is helpful. It gives him a direction where he can focus his research. Namely, 'how to run a program in kiosk mode' rather than 'how to create an infinite loop that cannot be cancelled.'"
    No... He already mentioned he wants to "e.g Interactive kiosk", so saying "properly kiosk it" isn't adding any new information.
    Yes it is. He asked how to do it using some Swing trickery, and I said it won't work. Which is true. The OP didn't already know that, clearly, hence the question. Now he does. That's pretty much new information if you ask me.
    Rather it's just giving the person a dead point of: you're doing it wrong.Right. He is. However, the problem is now no longer related to Java, and not something I can help him with. You should try that: not answering questions you don't know the answer to. It's actually more helpful than posting misleading, wrong answers.
    Pointing out someone is wrong is not a positive enlightenment. I'm giving basic technical help, not yoga lessons.
    Pointing someone in the right direction is more productive.As already stated, I am as ignorant of how to make a Java application into a kiosk app as the OP is. What value can I add in light of that information? I began by answering a question that was not stated to be about kiosks. Once the question moved outside of my area of knowledge, I stopped trying to answer.
    I notice you're not giving him any help, being more interested in admonishing others as you are.

  • Legacy C program - new Java GUI and calling methods

    Hello,
    I am fairly new to these forums. I've been working for a company that has an exsisting program that is a propriotery video-client to a video-server. It is written in C and has DirectX components for the standalone C application and ActiveX components for the Internet Explorer version.
    I looked over the source code and found a main() function. Now I am trying to execute that main() fuction through java and jni. I have managed to get a simple HelloWorld program to work but am now stuck.
    I would like to do the following.
    1. Convert the exsisting code to a C library. I have to use Visual C++ 6.0 to create this project. (There is already an exsisting dsw project file for the video client).
    Question: How do I get rid of the main() function and compile correctly in Visual C++?
    2. How do I call the library function that I created in the above from a java main() method? What should I use to edit and compile java/c code?
    Thank you,
    Viral

    Hi,
    If you need to have fast results you can try to use the ActiveX with COM bridge like Jawin or JACOb (both at sourceforge) or use Eclipse's SWT that has an activeX support too.
    An other alternative is to create a full JNI wrapper to your project.
    A1 : Replace the main by DllMain (create a new DLL with msvc++ wizard to see how to create a basic DLL).
    A2 : Call all functions exported by DLLs with a free tool like JNative.
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Help needed creating a linked list program

    I have been trying to create a linked list program that takes in a word and its definition and adds the word to a link list followed by the definition. i.e. java - a simple platform-independent object-oriented programming language.
    the thing is that of course words must be added and removed from the list. so i am going to use the compareTo method. Basically there a 2 problems
    1: some syntax problem which is causing my add and remove method not to be seen from the WordList class
    2: Do I HAVE to use the iterator class to go thru the list? I understand how the iterator works but i see no need for it.
    I just need to be pointed in the right direction im a little lost.................
    your help would be greatly appreciated i've been working on this over a week i dont like linked list..........
    Here are my 4 classes of code........
    * Dictionary.java
    * Created on November 4, 2007, 10:53 PM
    * A class Dictionary that implements the other classes.
    * @author Denis
    import javax.swing.JOptionPane;
    /* testWordList.java
    * Created on November 10, 2007, 11:50 AM
    * @author Denis
    public class Dictionary {
        /** Creates a new instance of testWordList */
        public Dictionary() {
            boolean done=false;
    String in="";
    while(done!=true)
    in = JOptionPane.showInputDialog("Type 1 to add to Dictionary, 2 to remove from Dictionary, 3 to Print Dictionary, and 4 to Quit");
    int num = Integer.parseInt(in);
    switch (num){
    case 1:
    String toAdd = JOptionPane.showInputDialog("Please Key in the Word a dash and the definition.");
    add(toAdd);
    break;
    case 2:
    String toDelete = JOptionPane.showInputDialog("What would you like to remove?");
    remove(toDelete);
    break;
    case 3:
    for(Word e: list)
    System.out.println(e);
    break;
    case 4:
    System.out.println("Transaction complete.");
    done = true;
    break;
    default:
    done = true;
    break;
       }//end switch
      }//end while
    }//end Dictionary
    }//end main
    import java.util.Iterator;
    /* WordList.java
    * Created on November 4, 2007, 10:40 PM
    *A class WordList that creates and maintains a linked list of words and their meanings in lexicographical order.
    * @author Denis*/
    public class WordList{
        WordNode list;
        //Iterator<WordList> i = list.iterator();
        /*public void main(String [] args)
        while(i.hasNext())
        WordNode w = i.next();
        String s = w.word.getWord();
        void WordList() {
          list = null;
        void add(Word b)
            WordNode temp = new WordNode(b);
            WordNode current,previous = null;
            boolean found = false;
            try{
            if(list == null)
                list=temp;
            else{
                current = list;
                while(current != null && !found)
                    if(temp.object.getWord().compareTo(current.object.getWord())<0)
                        found = true;
                    else{
                    previous=current;
                    current=current.next;
                temp.next=current;
                if(previous==null)
                    list=temp;
                else
                    previous.next=temp;
                }//end else
            }//end try
            catch (NullPointerException e)
                System.out.println("Catch at line 46");
        }//end add
    /*WordNode.java
    * Created on November 4, 2007, 10:40 PM
    *A class WordNode that contains a Word object of information and a link field that will contain the address of the next WordNode.
    * @author Denis
    public class WordNode {
        Word object;//Word object of information
        WordNode next;//link field that will contain the address of the next WordNode.
        WordNode object2;
        public WordNode (WordNode wrd)
             object2 = wrd;
             next = null;
        WordNode(Word x)
            object = x;
            next = null;
        WordNode list = null;
        //WordNode list = new WordNode("z");
        Word getWord()
            return object;
        WordNode getNode()
            return next;
    import javax.swing.JOptionPane;
    /* Word.java
    * Created on November 4, 2007, 10:39 PM
    * A class Word that holds the name of a word and its meaning.
    * @author Denis
    public class Word {
        private String word = " ";
        /** Creates a new instance of Word with the definition*/
        public Word(String w) {
            word = w;
        String getWord(){
            return word;
    }

    zoemayne wrote:
    java:26: cannot find symbol
    symbol  : method add(java.lang.String)
    location: class Dictionary
    add(toAdd);this is in the dictionary class
    generic messageThat's because there is no add(...) method in your Dictionary class: the add(...) method is in your WordList class.
    To answer your next question "+how do I add things to my list then?+": Well, you need to create an instance of your WordList class and call the add(...) method on that instance.
    Here's an example of instantiating an object and invoking a method on that instance:
    Integer i = new Integer(6); // create an instance of Integer
    System.out.println(i.toString()); // call it's toString() method and display it on the "stdout"

  • How to create a step loop control in a screen

    Hi,
    Basically, what I need to do is to create a fully dynamic screen. I want to send the structure name and the fiednames and the function will create a screen will all the required fields.
    I explored many possibilities and the best one seems to be using the step loop control in a screen. I know, this is really old technology but I do not care as long as this work great !
    I saw that the function group MASSFREESELECTIONS is doing it, this look pretty simple, fast and fully dynamic...
    My only problem is that I'm not able to create a step loop control in a standard screen.... is it an obsolete function that was removed from the control list ?
    Thanks
    Dany
    Message was edited by:
            Dany Charbonneau

    Hi,
    STEP LOOPS
    Step Loops are type of screen table . Step loops are repeated blocks of field in a screen. Each block contains one or more fields and these blocks are repeated. Step loops aren’t like actual table. You can scroll vertically but not horizontally. Three steps are associated with creation of step loops:
    • Creation of step loops on screen, which includes declaring fields on the screen and then defining the step, loops for these fields.
    • Passing data to the step loop is exactly similar to the passing of data to table controls.
    • In step loop, you don’t need to define the step loop as such in the module pool program but the cursor needs to be defined in the program.
    Types of Step Loops
    • Static – Static Step Loop (SSL) have fixed size that cannot be changed during the runtime. If user resizes the window, the size of the static step loop is not changed.
    • Dynamic – Dynamic Step Loop (DSL) is variable in size. When the user resizes the window, the system increases or decreases the number of the step loop blocks.
    You can have only one dynamic step loop and can have as many static loops in your transaction.
    Programming with the Static and dynamic step loop is exactly same. For the system or for the user it doesn’t make any difference whether it is static or dynamic step loop. Only attribute, which you fix during designing of the step loop, is type attribute for step loop F for fixed i.e static and V for variable i.e. dynamic.
    Writing code for Step Loop in the flow logic.
    PBO.
    Loop at itab cursor cl.
    Module set.
    Endloop.
    PAI.
    Loop at itab.
    Endloop.
    Empty loop is must for both table control and step loop
    LOOP AT statement for step loops and Table controls is similar. Loop At statement transfers the data to screen table. You need to have the Module to assign the values for the screen table.
    In module pool program you need to define the cursor.
    Date: CL TYPE i.
    Cursor parameter tells which line of step loop display should start.
    “Module Set” in module pool program assigns the values to step loop fields, which is similar to table controls.
    Refer to this program <b>DEMO_DYNPRO_STEP_LOOP</b>
         hope u can get some idea about table control & step loop in this line.
    http://www.sapfans.com/sapfans/repos/comelite.htm
    Check this link,
    http://help.sap.com/saphelp_erp2004/helpdata/en/9f/dbac1d35c111d1829f0000e829fbfe/frameset.htm
    <b>Reward points</b>
    Regards

  • How can I create more than one program shortcuts in LabView Installer?

    I have builded 3 executable files in LabView Application Builder. I want to create an installer (using the same application builder) that can copy the 3 files to the same destiny directory, install the Runtime Engine and create (in the same program group) 3 shortcuts for the executables. Is it possible with LabView Installer?

    The Application Builder does not offer this option. It is really intended as a basic installer builder. However, you can use other software packages to enhance your isntaller. There are many third party installer builders that you can use to modify the msi file which the application builder create for you. You might want to check one of those out and use it for your application.
    J.R. Allen

Maybe you are looking for

  • Join Between PO and Requisition

    Hi, I am trying to do a report in Disco (4.1.48) on all the Open Po's associated Req with the Req Amount and Amount receipted. I can get the info for the Req fine and the info for the PO's fine. But I cannot get the join between the Po and Req to wor

  • Help for update query

    Hi, How I can split the column of code_full_desc and update in code1,code2 and code3 column which are blank. It should be split after first hyphen. create table #desc (code_full_desc char(20), code1 char(4),code2 char(4), code3 char(6)) insert into #

  • How to run expect script using SGD?

    Does any one know how I can run an expect script using the same executable SGD uses? I think the executable is /opt/tarantella/bin/bin/ttatcl, but it seems to be looking for an init file that I cannot find. Anyone tried this before? Thanks

  • Slow Broadband Connection - Likely IP Profile Issu...

     Hello, For an age I've had a rubbish broadband connection, but the hassle of call centres and engineer visits have put me off sorting it out.  In August I got frustrated enough to try to get it resolved - I have a couple of work/study things that ne

  • MessageType in JSP

    Hello! I want to display message at screen. Message type and text must get from bean. Write this code: <hbj:messageBar id="mBar" >              <%               mBar.setMessageType(MessageType.ERROR);               mBar.setMessageText(UsRegBean.getVa