0

So here is my code and while simply is not checking the condition, but the if condition is working. I dont have slightest idea what the heck is going on, because it all seems to me ok.

import java.lang.Integer;

public class Wielomian
{
        public double [] mnozniki;
        public int max;


        Wielomian(String ciag)
        {
         int tmp;

         int stopien;

         //int iteration=0;
         int poczatek=0;

         tmp=ciag.indexOf('^');
         stopien=Integer.valueOf(ciag.substring(tmp+1,tmp+2));
         mnozniki = new double[stopien+1];
         max = stopien;
         tmp=0;
                for(int i=1; i< ciag.length() ; ++i)// +1.0*x^3-1.0*x^1-2.0
                {
                        //System.out.println("some:"+i);
                        if(ciag.charAt(i)=='*')
                        {
                        //      System.out.println("some:k");
                                stopien=Integer.valueOf(ciag.substring(i+3,i+4));

                                int iteration=i;

                                while(((ciag.charAt(iteration))!='-')||((ciag.charAt(iteration))!='+'))
                                {
                                       // System.out.println("some2:"+iteration);
                                        --iteration;
                                        if (iteration<=0) break;
                                }
                                 poczatek=iteration;
                   mnozniki[stopien]=Double.parseDouble(ciag.substring(poczatek,i-1));

                        }


                }
        }

        public void wypisz()
        {
                int i;
                for(i=0;i<max;++i)
                {

                 System.out.println("x^" + i+":"+mnozniki[i]);
                }
        }

And the main class:

    public class lab03
    {
            public static void main(String args[])
            {

                   String n ="1.0*x^3-1.0*x^1-2.0";
                   Wielomian wx = new Wielomian(n);


                   wx.wypisz();


            }
    }

In pastebin: http://pastebin.com/sgpY2AuG http://pastebin.com/eqXHNk0N <- ignore line 35

3
  • Your loop condition is postconditioning, so you have wrong idea. Commented Mar 24, 2013 at 15:03
  • Please trim down your code to show only the relevant parts before posting. Commented Mar 24, 2013 at 15:06
  • Regardless of why the loop does not work, you might want to learn how to use the debugger to solve this type of question if you do not know what is going on. It is very rewarding. Commented Mar 24, 2013 at 15:07

3 Answers 3

1
((ciag.charAt(iteration))!='-')||((ciag.charAt(iteration))!='+'))

is always true, this is a classic mistake (coming from the confusion of "not x or y" actually meaning "not (x or y)" but most newbies code as "(not x) or (not y)", use && instead or put the negation outside the OR

((ciag.charAt(iteration))!='-')&&((ciag.charAt(iteration))!='+'))

or

!((ciag.charAt(iteration))=='-')||((ciag.charAt(iteration))=='+'))

see de Morgan's law

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is here :
while(((ciag.charAt(iteration))!='-')||((ciag.charAt(iteration))!='+'))

Try using this instead :

while(((ciag.charAt(iteration))!='-')&&((ciag.charAt(iteration))!='+'))

As you have not specified the exact problem, I am not sure about the answer. But try and check.

Comments

0
while ( A != 1 || A != 2) {
    //Do Something 
}

The while loop will only end if A == 1 && A == 2, which can never be true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.