Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

can anybody help

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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' "
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post what your specific problem is, and your attempt at a solution
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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' "
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first step is parsing the query. You need to extract the values between ~ and ~, then match those against the map values.
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i parse the query and retrive the data between ~ ~
 
Marshal
Posts: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Larsen

but i think its not working
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this code is working but it is hard coded

i want to get the result dynamically without any hard coding
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put the code I posted together, you will come up with this:

and the modified query is:
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic