Java Queries Here..

Bandu

Journeyman
^^ I hope thats not a school assignment.

Anyways, the logic is pretty simple.
(1) Have an outer loop iterate thru 0 to 4. End it with System.out.println().
(2) Have an inner loop iterate through 0 till i and print a single *.

The above steps will give you the first part of the pattern. Reverse the logic to get the lower half.

- Bandu.
P.S. There are other ways to do this and there might be more simpler ways as well.
Code:
for(int i = 0; i < 4; i++, System.out.println())
            for(int j = 0; j <= i; j++)
                System.out.print("*");

Edit: The thread subscription email that I received has a different pattern. If it is the exact opposite of whats seen in the thread, then u'll have to slightly modify the loop structures:
Code:
for(int i = 0; i < 4; i++, System.out.println())
  {
   for(int j = 4; j > i; j--)
    System.out.print(" ");
   for(int k = 0; k <= i; k++)
    System.out.print("*");
  }
 
Last edited:
OP
furious_gamer

furious_gamer

Excessive happiness
^^ I hope thats not a school assignment.

Anyways, the logic is pretty simple.
(1) Have an outer loop iterate thru 0 to 4. End it with System.out.println().
(2) Have an inner loop iterate through 0 till i and print a single *.

The above steps will give you the first part of the pattern. Reverse the logic to get the lower half.

- Bandu.
P.S. There are other ways to do this and there might be more simpler ways as well.
Code:
for(int i = 0; i < 4; i++, System.out.println())
            for(int j = 0; j <= i; j++)
                System.out.print("*");
Edit: The thread subscription email that I received has a different pattern. If it is the exact opposite of whats seen in the thread, then u'll have to slightly modify the loop structures:
Code:
for(int i = 0; i < 4; i++, System.out.println())
  {
   for(int j = 4; j > i; j--)
    System.out.print(" ");
   for(int k = 0; k <= i; k++)
    System.out.print("*");
  }

He just ask the logic, but u provide him the code.... :rolleyes:
 

Bandu

Journeyman
He just ask the logic, but u provide him the code.... :rolleyes:
I kinda disappointed Aniruddh the last time with his Electronic showroom thing, and he's really at a very early stage of learning, so I thought it might help him better this way than exchaning multitude of PM's and emails for a trivial thing as this.

- Bandu.
 

aniruddhc

Right off the assembly line
Bandu I need to make the pattern in a diamond shape... The digit forum fsoftware doesnt allow me to post it in a diamond way

i think i need 6 loops, but i am not sure...

your logic unfortunately does not work...
 

chandru.in

In the zone
Core Java == Java SE.

I feel the code snippet there is good enough (if you have already worked with JPA in Java EE apps).
 

Desi-Tek.com

In the zone
i think i found the solution EntityManager can only be injected in ejb
*saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=003857

i'll try with spring
 

chandru.in

In the zone
As the URL I provided clearly says, it is not injected but rather should be created by code.
Code:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("acme");
EntityManager entityManager =  factory.createEntityManager();
...
entityManager.close();
 

Desi-Tek.com

In the zone
ah it worked thanks

PHP:
package com.ocricket.entity;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.apache.log4j.Logger;

/**
 * 
 */
public class EntityManagerUtil {
    
    private static final EntityManagerFactory emf; 
    private static final ThreadLocal<EntityManager> threadLocal;
    Logger log = Logger.getLogger(EntityManagerUtil.class);

    
    static {
        emf = Persistence.createEntityManagerFactory("OCRICKETPU");         
        threadLocal = new ThreadLocal<EntityManager>();
        
}
        
    public static EntityManager getEntityManager() {
        EntityManager manager = threadLocal.get();        
        if (manager == null || !manager.isOpen()) {
            manager = emf.createEntityManager();
            threadLocal.set(manager);
        }
        return manager;
    }
    
     public static void closeEntityManager() {
        EntityManager em = threadLocal.get();
        threadLocal.set(null);
        if (em != null) em.close();
    }
    
    public static void beginTransaction() {
        getEntityManager().getTransaction().begin();
    }
    
    public static void commit() {
        getEntityManager().getTransaction().commit();
    }  
    
    public static void rollback() {
        getEntityManager().getTransaction().rollback();
    } 
    
    public static Query createQuery(String query) {
        return getEntityManager().createQuery(query);
    }
    

    
}

PHP:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="*java.sun.com/xml/ns/persistence"
    xmlns:xsi="*www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="*java.sun.com/xml/ns/persistence
    *java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="OCRICKETPU"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.connection.driver_class"
                value="org.postgresql.Driver" />
            <property name="hibernate.connection.url"
                value="jdbc:postgresql://localhost:5432/ocricket" />
            <property name="hibernate.connection.username"
                value="postgres" />
            <property name="hibernate.connection.password"
                value="000000" />
            <property name="databasePlatform"
                value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.use_sql_comments" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />


        </properties>
    </persistence-unit>

</persistence>
 
Last edited:

dashang

Journeyman
I am looking for code to create login java program in which user enter pass if its correct run remaining program else terminate/ Help me guys. I am having problem with whowing '*' as display how to do it???
 

chandru.in

In the zone
You have not mentioned the type of app you are creating. So I'm suggesting for both desktop and web apps.

Desktop App:

Use the JPasswordField swing component.

Normal Web App:

Use <input type="password" />

JSF Web App:

Use <h:inputSecret />
 

dashang

Journeyman
Hey chandru can you give of full code from importing classes till end. Just give me password match code that is code for " getting pass and matching it and showing stars in pass while writing(optional)
 

chandru.in

In the zone
Hey chandru can you give of full code from importing classes till end. Just give me password match code that is code for " getting pass and matching it and showing stars in pass while writing(optional)
Going through the TLD docs and Java docs of the class and Tag I provided you should clarify everything for you.
 
Top Bottom