Jump to content

I need your help guys..

- - - - -

  • Please log in to reply
8 replies to this topic

#1
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
Hi im a newbie for java can u help me this kind of problem.the problem goes like this,The problem that is given to our teacher is like this design a parser that parse the given string and counts the number of characters that match the given data .Using stack and queue the output should be like this

given string: A B C D E F G
input string : a c c d e f c
no.of matches : 5

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
you have a list or array of given Strings g
you have a String containing the input string called s

the code could look a little bit like this (temp is a list of String):

for every element x of s

    if(g.contains(x) && !temp.contains(x))

        temp.add(x);


return temp.size();


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I'm not sure whether the string must contain each letter, or the position must be the same. In your example it's 5 for both ways ^^

#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

oxano said:

I'm not sure whether the string must contain each letter, or the position must be the same. In your example it's 5 for both ways ^^

Right, I didn't notice that. Of course my example works for the first hypothesis.

#5
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
import java.util.Stack;


public class machineproblem1
{

public static void main(String [] args){


Stack s = new Stack ();

s.push("A");s.push("B");s.push("C");

s.push("A");s.push("t");s.push("C");



System.out.println(s);
if(s.peek()=="t");
{
    System.out.println(" "+ s.size());
}
if(s.size()==0){

    System.out.println(" stack is empty");
}else{
    System.out.println("Stack Contains" + s.size() + " items");
}

while (!s.empty()){

    System.out.println(s.pop());


}
}
}
i try to solve it last night but the problem is ..is how to compare the input character to the exsisting character..I need your help guys to come up with e exact and right logic or flow of program..coz im a newbie for this thing

Edited by Alexander, 11 November 2010 - 03:47 PM.
(bbcode tags)


#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Please explain exactly what your program is supposed to do

#7
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
import java.util.*;

public class StackDemo{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new String("a"));
stack.push("a");
stack.push("b");
System.out.print("Given String");
System.out.println(stack);



i don't know the next procedure on how to compare the input string to the given string..can u help me how?

input string : a c c d e f c
no.of matches : 5

#8
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
import java.util.*;

class StackParse{


public static void main(String[] args){
Scanner console=new Scanner(System.in);
String given, inputstr;
String indexes="";
int cntfind=0;

System.out.println("Given/Input a string:");
given=console.nextLine();

System.out.println("Input a string:");
inputstr=console.nextLine();

Stack stack=new Stack();
//stack.push(new String());
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
stack.push("F");
stack.push("G");

System.out.println((stack));
//parse here using string(array of character)

for(int i=0;i< given.length(); i++){

for(int m=0;m< inputstr.length(); m++){
if(inputstr.charAt(m) == given.charAt(i)){
cntfind++;
indexes+=i+",";
}

}

}

System.out.println("Input String :"+inputstr);
System.out.println("NO. OF MATCHES :"+cntfind);
System.out.println("Indexes OF MATCHES :"+indexes);



}


}
I have already the solution but the problem is..how to implement this using stack...

#9
Zac Douglas

Zac Douglas

    Newbie

  • Members
  • Pip
  • 1 posts
import java.util.*;

class StackParse{


public static void main(String[] args){
Scanner console=new Scanner(System.in);
String given, inputstr;
String indexes="";
int cntfind=0;

System.out.println("Given/Input a string:");
given=console.nextLine();

System.out.println("Input a string:");
inputstr=console.nextLine();

Stack stack=new Stack();
//stack.push(new String());
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
stack.push("F");
stack.push("G");

System.out.println((stack));
//parse here using string(array of character)

for(int i=0;i< given.length(); i++){

for(String m:inputstr){
for (int n:stack){
if(stack.equals(inputstr.charAt(m))){
cntfind++;
indexes+=i+",";
}
}


}

}

System.out.println("Input String :"+inputstr);
System.out.println("NO. OF MATCHES :"+cntfind);
System.out.println("Indexes OF MATCHES :"+indexes);



}


}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users