aspose file tools*
The moose likes Beginning Java and the fly likes can anybody help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can anybody help" Watch "can anybody help" New topic
Author

can anybody help

sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
Hi this is srinivas . i have an assignment can anybody help


Here is the assignment.

public String MethodName(String query, List listValues)
{
//Here your code

}

There should be a test method i.e main in the class you are going to create
Tasks to be done:
1.Query =" VouchNumber='~VouchNum~' AND BusinessUnit='~BU~' AND InvoiceNumber='~IN~' "
HashMap will contain key value pair all strings VouchNum=00005, BU=0231,IN=3443
Output should be
" VouchNumber='00005' AND BusinessUnit='0231' AND InvoiceNumber='3443' "
2. Query =" VouchNumber='~VouchNum~' AND BusinessUnit='~BU~' AND InvoiceNumber='~IN~' "
HashMap will contain key value pair all strings VouchNum=00005, IN=3443
Output should be
" VouchNumber='00005' AND InvoiceNumber='3443' "
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
post what your specific problem is, and your attempt at a solution
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
actually query is a string
String query =" VouchNumber='~VouchNum~' AND BusinessUnit='~BU~' AND InvoiceNumber='~IN~';
HashMap m contains the key-value pairs as
VouchNum=00005, BU=0231,IN=3443

iam passing HashMap object and query as arguments to the MethodName()
as MethodName(String query,HashMap m)

now i have to get output as

" VouchNumber='00005' AND BusinessUnit='0231' AND InvoiceNumber='3443' "
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Your first step is parsing the query. You need to extract the values between ~ and ~, then match those against the map values.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
how can i parse the query and retrive the data between ~ ~
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to JavaRanch. There are lots of different ways to parse that String. Look through the String class, the Scanner class, and the Java Tutorial about Regular Expressions.
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
thanks for the support
can you help out this
how can i retrive one by one key values from the HashMap and compare it with string variable
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You would have to get a Set of the Map.Entry objects then iterate through it for the key, but that isn't how you use a Map at all.

You "put" the key and value in, then you "get" with the key and the value reappears.
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37

i tried in this way it is workin almost with some extra stuff
i need to print only resultant string

[edit]Add code tags and remove excess whitespace. CR[/edit]
[ October 07, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Good progress, then?

Please use a more informative subject title in future, and find out about code tags; I have added tags so you can see how much easier your code is to read.

cR
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
thanks Campbell Ritchie

can you please help me out to get this output


" VouchNumber='00005' AND BusinessUnit='0231' AND InvoiceNumber='3443' "

from the above code .
iam getting an updation result
please help me out
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If you put the query in a StringBuffer, then you will be able to replace strings by there start and end indexes.

and then find the start and end index's of the first and second ~, then you will be able to find the key's used to get the values from the Map m

now you need to iterate over the query until no ~ are left


Regards, Rene Larsen
Dropbox Invite
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
thanks Larsen

but i think its not working
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
[code]
import java.io.*;
import java.util.*;

public class FA1{

public static void main(String args[]){
String a="",b="",c="";


/* String to be splitted. */
String query = " VouchNumber='~VouchNum~' AND BusinessUnit='~BU~' AND InvoiceNumber='~IN~'";
String[] temp;

/* delimeter */
String delimeter = "~";

/* given string will be splitted by the argument delimeter provided. */

temp = query.split(delimeter);

/* print splitted substrings */

for(int i =0; i < temp.length ; i++)
System.out.print(temp[i]);



//map code starts here
Map m = new HashMap();

// mapping some data in the map
m.put("VouchNum", "00005");
m.put("BU", "0231");
m.put("IN", "3443");



// iterating the map by it's keys
Iterator iterator = m.keySet().iterator();
//System.out.println("\n\n\n");

while (iterator.hasNext())
{
int i=0;

String key = (String) iterator.next();
// System.out.println(" " + key );


if( key.equals(temp[1]))
{
a=(String)m.get(key);
}

if( key.equals(temp[3]))
{
b=(String)m.get(key);
}

if( key.equals(temp[5]))
{
c=(String)m.get(key);
}
}
System.out.println("\n");

System.out.println(temp[0]+""+a+""+temp[2]+""+b+""+temp[4]+""+c+"'");

}

}
this code is working but it is hard coded

i want to get the result dynamically without any hard coding


[code]
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37


this code is working but it is hard coded

i want to get the result dynamically without any hard coding
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37



this code gives the result dynamically but the output is somewhat confusing

please help me out to get the output dynamically
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If you put the code I posted together, you will come up with this:

and the modified query is:
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
Thanks for your support and code Rene Larsen

finally i tried using Strings and i got the result
the code is



Once again thanks for your support
sudhasrinivas pallam
Ranch Hand

Joined: Oct 06, 2008
Posts: 37
Thanks a lot for support

i have another case where i have to get out put like this

" VouchNumber='00005' AND InvoiceNumber='3443' "

and the case is
CASE2:-
Query =" VouchNumber='~VouchNum~' AND BusinessUnit='~BU~' AND InvoiceNumber='~IN~' "
HashMap will contain key value pair all strings VouchNum=00005, IN=3443
Output should be
" VouchNumber='00005' AND InvoiceNumber='3443' "

help me in getting this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Is this a College assignment? This is getting more and more difficult for you.
Suggest you are going to have to pass the tokens you are looking for as parameters somewhere, so your method actually looks for the preceding split String matching "VouchNumber" or "InvoiceNumber".

You can pass a String[] with those tokens in, look for them in the String, find the ~ after that, get the contents of the ~---~ and put them together.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anybody help
 
Similar Threads
i need some other logic
get value of xml element at runtime in XSL
need help
hibernate one HashMap with different types of values.
Which java Collections Shall i Use?