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?!
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.
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.
Re: Need Help Finishing My Code!!
Start by reading up on Lists. Try this tutorial
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?
Re: Need Help Finishing My Code!!
Quote:
//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.
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()){
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.
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;
}
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
Re: Need Help Finishing My Code!!
yea thats true, but the program still doesnt run right...any other suggestions?
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..
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;
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.
Re: Need Help Finishing My Code!!
How would I correct that?
Re: Need Help Finishing My Code!!
Any luck yet? Ive tried multiple things getting the same result. The min & max == the same thing
Re: Need Help Finishing My Code!!
Re: Need Help Finishing My Code!!
Did you ever figure it out before the end of the night?
Re: Need Help Finishing My Code!!
So in your buildList method what is the following line supposed to do:
Code:
while(i<line.length() && line.charAt(i)!='i');
First time through the outer loop when this line is executed i = 0 so the first part of the while test (i < line.length()) will be true and the second part of the while test will always be true because 'line' contains digits and not a character 'i'. This means the while loop's code block will execute, but there is no code block - the line ends with a semi-colon - so the while loop test will execute again ie you will get stuck in an infinite loop.