last day (16 days later) » 
01:00 - 22:0022:00 - 00:00

1:44 AM
0
A: Running while loop to create linked lists in java

simchonaSo, you've implemented your own linked list using nodes. You have the right idea so far: for each line, you separate each word in the line and do something with it. Because you need to implement the pointers that connect each Node, here is the main thing you might consider doing: Figure out a way...

 
I have edited the question with the code... you can see it and you will understand better what my question is.
 
user830639
I'm not sure what you mean by 'with code'. You only mentioned you can't use arrays...?
 
I just added the code and I told you that before I added it. I am sorry about that...now you can see the code
 
user830639
@user Because this is homework, I'm not going to write the code for you. Instead, most people here will just give you hints.
 
I asked a question almost a month ago and people did give me hints at first so I understand. You can see the code so it would be great of you to help me with some hint or any form of help.
it seems you are gone.
 
user830639
1:44 AM
I apologize, I have a bit of homework of my own. If you don't get any good answers soon, I'll spend some time giving you whatever hints I can.
 
ok, well I will wait for 10 more minutes and then reply back here, please do check
 
user830639
I edited; I hope it is enough hint to get you started
 
I am sorry it doesn't help...Please see me in chat.
you there?
 
user830639
Hello
 
user830639
1:45 AM
So would you like me to walk through some pseudocode?
 
well, I need to get this done but I want to learn it too
 
user830639
Ok, so your loop should roughly be:
 
I don't have much time on this and it needs to finished now but I don't want to just get it done with, I have to learn this
 
user830639
for each line in the file:
 
user830639
Node parent = null
 
user830639
1:46 AM
(this is the "pointer" I meant)
 
user830639
for each word in the line:
 
user830639
Create a Node with that word. If parent is null, set parent to this first Node because you've found the first word in the sentence.
 
user830639
If parent isn't null, set the parent.next() pointer to that new Node, and then set parent to your new Node
 
user830639
Continue doing that for all your words in the line, and as the code continues you'll be setting pointers so that you're extending your list
 
wait a minute
i am a little confused
so i need two nodes?
one pointer and one regular?
 
user830639
1:53 AM
Well, you need a parent pointer Node outside the loop so that you can store whatever the previous word was
 
user830639
and then another Node that you create for every word inside your loop
 
is it parent or pointer?
  dataInpt=new File("C:\\sentences.txt");
  inFile=new Scanner(dataInpt);
  StringTokenizer myTokens;
  String line, sentence;
  Node parent = new Node();
  while (inFile.hasNextLine())
  {
    line=inFile.nextLine();
    myTokens=new StringTokenizer(line);
    while (myTokens.hasMoreTokens())
    {
      Node current = new Node();
      current.value=myTokens.nextToken();
      if(parent==null)
        parent.value=current.value;
      else
      {
        parent.next=current;
        parent=current;
that is the code i have
and it prints this out
you
name
do
hello
up
Press any key to continue...
here is sentences.txt by the way:
Hi how are you
what is your name
what would you like to do
hello
whats up
you there?
 
2:15 AM
@EthanSteinberg can you please help me?
 
user406009
What the heck. I have time to spare.
 
user406009
Let me read the whole discussion thing.
 
user406009
Can you post your whole code on ideone or something?
 
user406009
2:19 AM
Lines 23-24 look odd. Why are you checking if the parent is null?
 
user406009
Shouldn't the parent never be null?
 
the person who was here before you told me this
what do you think should be changed?
 
user406009
Well you are trying to create a linked list of the works right?
 
user406009
Is your target list going to be all the lines added together? Or are you going to have seperate lists for each line?
 
well i am running a while loop
that goes through each line and puts it in linked list then prints that linked list out and then moves on to the next one
 
user406009
2:27 AM
The question is: do you want a seperate linked list for each row in the table, or one linked list that stores all the rows?
 
user406009
Right now you only create a new root node once, so everything is all in one giant linked list.
 
latter
but i am not storing in linked list
 
user406009
Well the reason why your code is not working right now is that you are losing your reference to the head.
 
user406009
Perhaps it will work with something like this(ideone.com/hOGQK)?
 
user406009
Note lines 18 and 30
 
2:33 AM
hold on a sec
well here is the thing
The task that I have to do is different, part of it is what I showed you here, its not that hard...just a little thinking
 
user406009
Guessing on the name, you have to make some sort of calculator?
 
no
so there is an external txt file that has polynomials per line
the task is to choose two lines at a time
using string tokenizer, get the co-efficient and exponents for each part of the polynomial and put it in a linkedlist
finally last thing is to take the two linked lists and add them up
you there?
you probably left because of the difficulty of the task and I understand, but I really need help on it and if you are still there, please help me
you there?
 
user406009
Sorry sort of back and forth between this and other stuff.
 
user406009
Not quite sure the best way to parse this type of thing in java.
 
that's perfectly all right
well
the main part is capturing two lines in java putting them in linked list
so the same concept applies here that i first told you about
 
user406009
2:48 AM
Don't you already have your linked list generating code?
 
most of it
 
user406009
Did my suggestion make it work?
 
So I have the code for capturing the co-efficient and exponenent
exponent*
and now i just need to run it in a while loop so a node is added with co-efficient and the corresponding exponent until that line ends
 
user406009
Why don't you move your list generatin loop into a function?
 
user406009
Wait a sec and I can show you what I am thinking.
 
2:53 AM
yes
I am using your code that you gave me
Now I need to run that for two different lines at once
 
user406009
So you move it into a function like ideone.com/gisft
 
user406009
Then all you need to do to run it on two lines at once is to call the function twice.
 
user406009
 
user406009
Then you could probably add some code around line 23ish that does:
firstList = convertToNumbers(tempParent);
secondList = convertToNumbers(secondParent);
sumOfLists = addLists(firstList,secondList);
printNumberList(sumOfLists);
 
I have a question
 
user406009
2:59 AM
Yeah?
 
look at this
oh wait no
look at this
wait
ideone.com/ewKus look at lines 22-31, what is the point of those lines?
 
user406009
Node current = new Node(); // Creates a new node to store next result
 
user406009
current.value=myTokens.nextToken(); // Stores value of next token in node.
 
user406009
if(parent==null) // Checks if parent == null( never going to happen)
 
yes but all you do i print out tempParent
  Node parent = new Node();
  while (inFile.hasNextLine())
  {
    Node tempParent = parent;
    line=inFile.nextLine();
    myTokens=new StringTokenizer(line);
    while (myTokens.hasMoreTokens())
    {
      Node current = new Node();
      current.value=myTokens.nextToken();
      if(parent==null)
        parent.value=current.value;
      else
      {
        parent.next=current;
        parent=current;
      }
    }
    printList(tempParent.next);
tempParent = parent
and then you print out tempParent.next
 
user406009
3:06 AM
Remeber that inbetween "tempParent = parent" and the printing there are a couple "parent = current" stuff going on.
 
user406009
So the parent at the printing is not the same parent at the start.
 
user406009
I print out` tempParent.next`, not tempParent because I want to start printing at the first new node.
 
user406009
That code is still there, but like I said the whole "if(parent==null)
parent.value=current.value;" is pointless since parent is never null in this case.
 
user406009
The `parent.next=current;
parent=current;` is the important part as it resets the parent and adds your new node to the list.
 
3:08 AM
ok so i changed the node class so it can have another value:
public class Node
{
public Object value;
public Object value2;
public Node next;

public Node()
{
value=null;
value2=null;
next=null;
}
public Node (Object value, Object value2, Node next)
{
this.value=value;
this.value2=value2;
this.next=next;
}
}
 
user406009
Ok, that looks fine.
 
so
I have this code
.value=polyTerm.substring(0,polyTerm.indexOf("x"));
.value2=polyTerm.substring(polyTerm.indexOf("^")+1);
polyterm is equal to the String that is created using stringtokenizer
 
user406009
Ok.
 
wait a sec
here is the output
2 4 -5 3 +9 2 -10 0
3 4 -6 3 +10 2 -11 0
-2 1 +4 0
2 1 -4 0
8 5 +6 4 +5 2 -3 0
-12 8 +2 7 +14 5
1 5 +7 2 +8 1
-5 4 -7 3 -4 2 -3 0
10 5 -3 3 +4 2 -234 1 -12 0
-5 5 -2 3 +10 0
Press any key to continue...
 
user406009
Well is that correct?
 
3:18 AM
yes
 
user406009
So what's the issue?
 
that is the co-effeicient and exponent pair side by side
no i need to add them up
 
user406009
Are the exponent's guaranteed to be in order? Can I see the input file?
 
2x^4 -5x^3 +9x^2 -10x^0
3x^4 -6x^3 +10x^2 -11x^0
-2x^1 +4x^0
2x^1 -4x^0
8x^5 +6x^4 +5x^2 -3x^0
-12x^8 +2x^7 +14x^5
1x^5 +7x^2 +8x^1
-5x^4 -7x^3 -4x^2 -3x^0
10x^5 -3x^3 +4x^2 -234x^1 -12x^0
-5x^5 -2x^3 +10x^0
that is the file
 
user406009
So it does look like things are not guaranteed in order.
 
user406009
3:20 AM
Do you already have a formula for adding them?
 
now they need to be added up
well here is the thing
if they are same exponents then they can be added
else you print both of the polynomials out
which is the fun and hard part
 
user406009
This is my idea for a formula to add them:

Start with two nodes. A node pointing to the front of each lists.
Compare said nodes.

If equal, add them together and print. Then advance both nodes by one.

If one is greater than the other, the greater one is printed out and that node is advanced.

If one end of the list is reached, simply print out the rest of the other list.
 
user406009
(Actually, the polynomials are in order, just not the same polynomials for each time.
 
ok
now i will tell what i thought
i was thinking that as I keep creating new nodes when using string tokenizer to scan through the line, I can add and print them up right away
 
user406009
Not really. When you scanning through line one how are you to know what to print for line two?
 
3:27 AM
then what is the best way to do it?
I am little confused by your formula, could you please explain a little bit more
 
user406009
Grab line one into one list. Grab line two into another list. Run through formula to print out combination.
 
user406009
(My formula is actually a blatant copy of merge sort)
 
how would i grab the second line?
 
user406009
Running the same code as to grab the first line, but storing in a different list.
 
Could you please give me an example
 
user406009
3:31 AM
Here a walkthrough of my formula:

Start with
1x^5 +7x^2 +8x^1
-5x^4 -7x^3 -4x^2 -3x^0

starts with 1x^5 , -5x^4.
5 > 4, so print 1x^5.

Now 7x^2, -5x^4.
4 > 2, so print 5x^4

Now 7x^2, -7x^3
3 > 2, so print -7x^3

Now 7x^2, -4x^2
2 ==2, so print 3x^2

Etc.
 
user406009
@user1079641 an example of grabbing the list would be ideone.com/LrNby.
 
user406009
Just copied and pasted the code, but changed the name of list where it is stored.
 
gives an error no such element exception
you there?
 
user406009
Yeah.
 
user406009
No idea why it would give that error.
 
3:43 AM
here is the code
does anything look wrong?
 
user406009
Don't know.
 
I know
 
user406009
Java should tell you the line it fails at.
 
because if you look at the code
 
user406009
Got to go anyways.
 
user406009
3:47 AM
Hope you finish.
 
its last thing
 
4:02 AM
What's the problem?
 
4:16 AM
@LeviMorrison hey l
 
hello
 
0
Q: Token based database driven parent child navigation

epic_syntaxLets say we have 2 tables Table Pages: id | short_name | long_name | token 1 | Mail | My mail box | mail 2 | All mails | All mails | all 3 | Inbox | Inbox only | inb 4 | Users | Users | users 5 | All users | All users | all and table navigation: id ...

 
I'd do this the StackOverflow way: still use the ID but append the long_name to the url.
For example, /page/1 becomes /page/1/my-mail-box. The way it works is that you first rewrite /page/1/my-mail-box to /page.php?id=1&long_name=my-mail-box. Then, you look up the information for page 1. If the long names don't match, then rewrite the URL to what it should be. So if I give /page/1/all, it would redirect me to /page/1/my-mail-box.
Savvy?
 
 
15 hours later…
7:29 PM
anyone here?
anyone?
is anyone here who can help me?
 
Not yet. But just to say: smart move to start your own room
 
no no
i wasn't trying to start my own room.
someone else was helping me yesterday but had to leave...and so I am asking someone else to help me
you can close this room when this problem is solved...The whole point is to help me as no one responds in java room
 
@user1079641 I wasn't being ironic! I mean, really a smart move to start your own room. You were probably annoying more than one C++ guy with java business, and the people that are interested can follow you here (like me)
 
listen, if you scroll up the room, you can see someone else helped on SO and we decided to move to chat because of so many comments. He ended up leaving and I was left alone. Since this room is still open, I thought I will ask someone else to help me instead of me going everywhere posting a question
 
so: good idea.
Listen back, do you have an input file?
What is the current question, in a single line of text?
 
7:52 PM
its a little complicated to express the question in a single line
I have a while loop that collects polynomial on a line from an external file and separates them using string tokenizer into linked list where each linked list contains two slots: one for coefficient and one for exponent. I have code that does the job of breaking the polynomials into linked lists. The final part is to add two lines of polynomials together using linked lists after the values have been put into linked lists (already done)...I will give you the code of what I have till now.
 
And the newest Node class, then, because I can't cast that to int, you know
 
here is the latest node class: ideone.com/NAGlv
 
8:08 PM
how could this work then? System.out.println(((int)tempParent)+((int)tempParent2));
you can't just cast Node instances to int
 
i made those changes, the person who was helping me earlier gave me this code: ideone.com/LrNby
you there?
@sehe are you there?
 
8:27 PM
sort of
 
well are you doing anything on the code? if not then tell me and I will seek someone else.
 
@user1079641 do that anyway :) I'm not a java programmer, remember. I was looking at it, but you didn't tell me what the remaining question is. Do start with the working code
 
i told you the remaining question
you have two nodes, each for one line totaling 2 lines and the objective is to add the link list contents up
 
8:41 PM
I have no clue what nodes, and 'add the link list contents up' isn't exactly a clear specification, but perhaps the following helps a bit: ideone.com/RPXCh
It at least parses the actual values into actual numerical values
What's the deal with these lines (twice):
      if(parent==null)
        parent.factor=current.factor;
 
i can't
i am not allowed to use public float
or public integer
 
What is a public float anyway? You make it sound like a promiscuous lady
 
public Float factor; I can't use that
 
public class Node
{
  Float factor;
  Integer exponent;
  Node next;
 // ...
}
FTFY
 
i can't use float
i am not allowed to
 
8:48 PM
Ok, where is the list of random constraints
 
9:10 PM
?
 
@user1079641 that's clear as peaches. Not being able to use java inside java is a ridiculous constraint. A linked list, ok, but language intrinsics? Why. Any more of these constraints?
 
You probably know I am a student
if not then I am a student
I can't use what I haven't learned yet
the code that i am restricted to can be seen by looking at the code i have given you
 
@user1079641 That's a good way not to learn stuff there.
 
I am willing to learn, but I can't help it if my teacher doesn't want me to use it
 
I'm not willing to accept that you cannot use float. It flies in the face of logic. Just tell your prof. If he wants you to parse floats from text and calculate using them, he would have given you a different exercise.
@user1079641 Oh yes you can. If you want to learn, you'll have to have an opinion. Just because you don't get it, is not a reason to do nothing (about it).
 
9:22 PM
I can learn about it personally but I can't use it on an excercise that my teacher has given as he won't accept it
 
@user1079641 Good, so I can help you with what I have, and you just figure out a way to please your prof. That is a whole different study, and I'm not taking part of it
 
?
All i asked for is help in particular part
I am not going to accept it from you that people here can't do this without using all these variables because in the past, i have had people help me without using all these things
 
@user1079641 Are you saying you don't want my help? That ok with me
 
I am saying that this question doesn't require all these things that you are telling me about, if it did, then the teacher wouldn't have given us all this
 
So maybe you can explain me a little bit more about 'adding linked list content up'
So perhaps I can understand why I don't need those things. (Or I can explain why you do)
 
9:31 PM
the project is to run a while loop
there is an external file
with polynomials on each line
like 5x^3 -3x^2 + 1x^1
I have to create a program that scans through the file
and takes two lines in
converts each line into linked list using string tokenizer
As you can see, in Node class, I have two value fields
one for co-efficient and one for expoenent
i have to create a linked list for the two lines
when that is done
I need to code the program so it can add up the polynomials
the polynomials are in the linked list
 
Meanwhile, here is stuff a bit cleaned-upped: ideone.com/RPXCh
 
5x^3 -3x^2 = Node1(value=5, value2=3) and Node2(value=-3, value2=2)
 
@user1079641 Do you have a definition of adding polynomials? I don't add polynomials everyday
 
if each part of the polynomial has same exponent, you add the co-efficients up and print them out
 
@user1079641 Look at my code, I got that far. I even renamed the fancy variables value and value2 to actually describe those things. (factor and exponent)
@user1079641 Otherwise, merge them, I guess. Ok, here goes
 
9:36 PM
if they don't have the same exponents, you print out both parts of polynomials
yeh
 
@user1079641 Oh, and by the way, yes you need flioating point addition, and there is no way in hell your prof expects you to write that without floats. Perhaps you know decimals or bignum?
@user1079641 Great, so we need to sort by exponent, and merge the list, adding the factors for terms with equal exponent?
 
I can't use floats
 
See now we're getting somewhere: a spec. (the project is to run a while loop is a bit ludicrous)
 
wait a minute i will give you word by word project specifications
 
@user1079641 Like I said, tell your prof that. I don't really care, and I don't believe it. Reasons stated ^^
 
9:38 PM
here it goes
 
@user1079641 Sounds like fun
 
Maybe I am not explaining right
here it is:
"It is possible to use a linked list to represent a polynomial, with each node containing three fields - coefficient, exponent, and reference. Consequently, it becomes possible to conduct polynomial addition using three linked lists, one for each polynomial expression and a third for the result of the addition. continued ->
For purposes of this assignment, we will assume that x will always be the variable used. A data file called llpoly.txt has been established containing 5 pairs of polynomials to be added. Since there are 10 lines in the datafile, we will add adjacent polynomials. (The polynomial on line one will be added to the polynomial on line 2...line 3 will be added to line 4...etc.) The construction of each polynomial has been standardized according to the following scheme: -3x^4 +7x^3 -5x^1 +10x^0"
 
So far so good
Oh you mean the factors are always going to be integral.
I definitely remember seeing decimal points in the sample in your question from yesterday, not? Perhaps I misremembered. Anyways, nobody said the factors will be integral, but we can assume it just for fun.
 
1. The ^ is always used to indicate exponentiation even in cases of an exponent of 1 or 0
 
Why the heck didn't you just say, you can replace float by int because the factors are integers? Save me a lot of energy :)
 
9:47 PM
I am sorry, now that I gave you the word-to-word directions, I don't think there should be anymore problems
If you have any questions, please ask me. I am more than happy to answer. You are already doing so much to help and I appreciate it
I just need to get this done
you there or working on that code?
you there?
 
back
@user1079641 can we depend on the input polynomials to be ordered by descending exponent? That would eliminate the sorting step
So that leaves merging the lists.
 
oh hey, you again
 
01:00 - 22:0022:00 - 00:00

  last day (16 days later) »