|
-
November 22nd, 2011, 01:12 AM
#1
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?!
-
November 22nd, 2011, 06:25 AM
#2
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.
-
November 22nd, 2011, 01:48 PM
#3
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.
-
November 22nd, 2011, 04:17 PM
#4
Re: Need Help Finishing My Code!!
Start by reading up on Lists. Try this tutorial
-
November 22nd, 2011, 05:00 PM
#5
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?
-
November 23rd, 2011, 05:40 AM
#6
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.
-
November 29th, 2011, 09:04 PM
#7
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()){
-
November 30th, 2011, 06:28 AM
#8
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.
-
November 30th, 2011, 05:46 PM
#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;
}
-
November 30th, 2011, 08:13 PM
#10
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
-
November 30th, 2011, 08:45 PM
#11
Re: Need Help Finishing My Code!!
yea thats true, but the program still doesnt run right...any other suggestions?
-
November 30th, 2011, 08:53 PM
#12
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..
-
November 30th, 2011, 09:15 PM
#13
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;
-
November 30th, 2011, 09:32 PM
#14
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.
-
November 30th, 2011, 09:35 PM
#15
Re: Need Help Finishing My Code!!
How would I correct that?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|