CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Nov 2011
    Posts
    9

    Need Help Finishing My Code!!

    I need some help with lists, im new to Java and have been struggling with them. This is what i need to do.

    private SimpleList<Integer> searchLine(String line, String s) {
    //Build a list of the beginning positions of s in line

    }

    private String buildLine(SimpleList<Integer> lineNums) {
    //Build a String from the integers in lineNums

    }

    Could someone please help me?!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need Help Finishing My Code!!

    The reason you are given homework is so you can learn from the experience of writing the code yourself. Getting someone else to write won't teach you anything.

    If you explain what you are stuck on we will try to explain it to you.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    This is our first list practice problem, I just need help getting it started then i should be able to figure out the rest myself.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need Help Finishing My Code!!

    Start by reading up on Lists. Try this tutorial
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    private SimpleList<Integer> searchLine(String line, String s) {
    //Build a list of the beginning positions of s in line
    SimpleList <Integer> list;
    list = new SimpleList <Integer>();
    int i = 0;
    int m = 0;
    int o;
    while (i<line.length()){
    o = i;
    m = 0;
    while (m<s.length() && o<line.length() && line.charAt(o) == s.charAt(m)){
    o++;
    m++;
    }
    if(m == s.length())
    list.add(i);
    i++;
    }
    return list;
    }

    private String buildLine(SimpleList<Integer> lineNums) {
    //Build a String from the integers in lineNums
    //See the assign ppts for more info
    String s = "";
    lineNums.reset();

    while (lineNums.hasNext()){
    s = s + " " + lineNums.next();
    }
    return s;

    }

    This is what I got...what do you think?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need Help Finishing My Code!!

    //Build a list of the beginning positions of s in line
    Why not use the String classes indexOf method to find the index of the string s in string line. There is a version of the method which allows you to specify a starting index so you can find subsequent instances.

    Code:
    //Build a String from the integers in lineNums
    When creating a string by repetitive concatenation you should use the StringBuider class.

    If SimpleList conforms to the standard List implementations (this isn't a standard class so I don't know what it does) then there won't be a next and hasNext methods. These method names are normally associated with an Iterator obtained from the list object. Rather than using an Iterator you could just use the for each loop construct to iterate over the list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    Yea your way would have been much simpler, but this is the way our professor is teaching us to do it.

    Here is another example that you might have a suggestion to, I have the start of it i believe:

    private SimpleList<Integer> buildList(String line) {
    //Build a list of integers from line
    //Assume the line is syntactically correct

    SimpleList<Integer> list;
    list = new SimpleList<Integer>();
    i=0;
    while(i<s.length()){

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need Help Finishing My Code!!

    Using while(i<s.length()) is OK if the string is full of single digit integers with no spaces, commas etc separating them.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    I dont think this is correct, I need some help badly.


    private SimpleList<Integer> buildList(String line) {
    //Build a list of integers from line
    //Assume the line is syntactically correct
    SimpleList<Integer> list;
    list = new SimpleList<Integer>();
    int i=0;
    while(i<=line.length()){
    int num=0;
    while(i<line.length() && line.charAt(i)!='i');
    num=i+(line.charAt(i)-'0');
    i++;
    list.add(num);
    i++;
    }
    return list;


    }


    private int findMax(SimpleList<Integer> list) {
    //Return the maximum integer in list
    int max;
    list.reset();
    max=list.next();
    while(list.hasNext()){
    int i=list.next();
    if(i>max);
    max=i;
    i++;
    }
    return max;
    }

    private int findMin(SimpleList<Integer> list) {
    //Return the minimum integer in list
    int min;
    list.reset();
    min=list.next();
    while(list.hasNext()){
    int i=list.next();
    if(i<min);
    min=i;
    i++;
    }
    return min;

    }

  10. #10
    Join Date
    Nov 2011
    Posts
    8

    Re: Need Help Finishing My Code!!

    One error that i found might be in findMax & findMin

    Im a little confused why you have the i++ in those methods

    I dont believe you need the i++.
    list.next looks at the int then automatically moves it to the next

  11. #11
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    yea thats true, but the program still doesnt run right...any other suggestions?

  12. #12
    Join Date
    Nov 2011
    Posts
    8

    Re: Need Help Finishing My Code!!

    What is suposed to be put in the delimiter text field? The problem is in the buildList some where, im still trying to figure it out..

  13. #13
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    the problem is in buildList, here is more of the code

    private String getLine(String s, int start) {
    String line = new String("");
    char ch;
    int i = start;
    if (start >= s.length()) return null;
    ch = s.charAt(i);
    while (ch != '\n') {
    line = line + ch;
    i++;
    ch = s.charAt(i);
    }
    line = line+'\n';
    return line;
    }

    private boolean isDelimiter(char ch) {
    String delims = delimiters.getText();
    for (int i=0; i < delims.length(); i++)
    if (ch == delims.charAt(i))
    return true;
    return false;
    }

    private int checkLine(String line) {
    char ch;
    int state = 0;
    boolean error = false;
    int i = 0;
    while (i < line.length() && !error) {
    ch = line.charAt(i);
    if ((state == 0 || state == 1) && Character.isDigit(ch))
    state = 1;
    else if (state == 1 && isDelimiter(ch))
    state = 0;
    else if (state == 1 && ch == '\r')
    state = 2;
    else if ((state == 2 || state == 1) && ch =='\n')
    state = 3;
    else
    error = true;
    i++;
    }
    if (error) return (i-1);
    else return line.length();
    }


    private SimpleList<Integer> buildList(String line) {
    //Build a list of integers from line
    //Assume the line is syntactically correct
    SimpleList<Integer> list;
    list = new SimpleList<Integer>();
    int i=0;
    while(i<=line.length()){
    int num=0;
    while(i<line.length() && line.charAt(i)!='i');
    num=i+(line.charAt(i)-'0');
    i++;
    list.add(num);
    i++;
    }
    return list;

  14. #14
    Join Date
    Nov 2011
    Posts
    8

    Re: Need Help Finishing My Code!!

    When I run it its always coming up with the min & max with the same value. I think buildlist is returning a list of the same value depending on what you enter & not the actual list of ints in the string.

  15. #15
    Join Date
    Nov 2011
    Posts
    9

    Re: Need Help Finishing My Code!!

    How would I correct that?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured