Author Topic: Infinite Loop  (Read 2428 times)

0 Members and 4 Guests are viewing this topic.

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Infinite Loop
« on: February 08, 2015, 08:28:17 pm »
Actually I am making a java project about store management just for some practice and I am stuck because of an infinite loop, the main problem i think is in last else or in while condition.What am I doing wrong?
Code: [Select]
public void itemList(){
        Scanner in=new Scanner(System.in);
        int cha1,cha2=0,fruits=10,veg=15,toy=15,groc=35,cha,am1=0;
        System.out.println("\n\n\n\n\nTo Add more Items:1\n\nTo View Existing Items:2");
        cha=in.nextInt();
        while(cha!='q')
        {
            switch(cha)
                    {
                case 1:
                    do{


                        System.out.println("\n\n\n\nEnter amount of:");
                        System.out.println("\nFruits:1\nVegetables:2\nToys:3\nGroceries:4");
                        cha1=in.nextInt();
                        if(cha1==1)
                        {
                            System.out.println("Fruits="+fruits+"\n\nEnter Amount:");
                            fruits+=(am1=in.nextInt());
                            System.out.println("New Amount:"+fruits);
                                            }
                        else if(cha1==2)
                        {
                            System.out.println("Vegetables="+veg+"\n\nEnter Amount:");
                            veg+=(am1=in.nextInt());
                            System.out.println("New Amount:"+veg);
                                            }
                        else if(cha1==3)
                        {
                            System.out.println("Toys="+toy+"\n\nEnter Amount:");
                            toy+=(am1=in.nextInt());
                            System.out.println("New Amount:"+toy);
                                            }
                        else if(cha1==4)
                        {
                            System.out.println("Groceries="+groc+"\n\nEnter Amount:");
                            groc+=(am1=in.nextInt());
                            System.out.println("New Amount:"+groc);
                                            }
                        else
                        {
                            System.out.println("Wrong Input \n\n"
                                    + "Enter 1 To Terminate or any other digit to Continue?");
                            cha2=in.nextInt();
                                            }
                        }
                    while(cha2==1);
                 
                    break;
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Infinite Loop
« Reply #1 on: February 08, 2015, 08:34:22 pm »
Code: [Select]
while(cha2==1);
This is your infinite loop. You basically say: While cha2 == 1 do nothing.
There is no possibility that cha2 will change its value.
Note that you put a ; there at the end, that ; is the single action you are doing in the loop. Remove the semicolon and you are fine.
Also: That's a reason not to omit the curly braces. Things like these shouldn't happen if you always add them. At least in Java it is convention that you always put braces.
« Last Edit: February 08, 2015, 08:37:50 pm by Deque »

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Re: Infinite Loop
« Reply #2 on: February 08, 2015, 08:42:25 pm »

There is no possibility that cha2 will change its value.
Note that you put a ; there at the end, that ; is the single action you are doing in the loop. Remove the semicolon and you are fine.
Also: That's a reason not to omit the curly braces. Things like these shouldn't happen if you always add them. At least in Java it is convention that you always put braces.
what I wanted to do was that if user inputs 1 the loop should terminate.
Code: [Select]
System.out.println("Wrong Input \n\n"       
+ "Enter 1 To Terminate or any other digit to Continue?");                           
cha2=in.nextInt();


« Last Edit: February 08, 2015, 08:47:57 pm by haseebr21 »
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Infinite Loop
« Reply #3 on: February 08, 2015, 08:44:50 pm »
what I wanted to do was that if user inputs 1 the loop should terminate.
Code: [Select]
System.out.println("Wrong Input \n\n"       
+ "Enter 1 To Terminate or any other digit to Continue?");                           
cha2=in.nextInt();

I know. As I said, remove the semicolon.

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Re: Infinite Loop
« Reply #4 on: February 08, 2015, 08:48:09 pm »
I know. As I said, remove the semicolon.
as for semicolon ';' the program is not running without it because a do statement must end with a while followed by ; :3

p.s Updated:The program did run without ; but still that was an infinite loop.



do
{

statements

}
while(cond);
« Last Edit: February 08, 2015, 08:51:35 pm by haseebr21 »
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Infinite Loop
« Reply #5 on: February 08, 2015, 09:11:04 pm »
My apologies that I overlooked the do.
The problem is the outer loop while(cha != 'q').
And also the condition while(cha2 == 1) should actually be while(cha2 != 1)

Here is a little fix just for demonstration:
Code: [Select]
Scanner in = new Scanner(System.in);
int cha1, cha2 = 0, fruits = 10, veg = 15, toy = 15, groc = 35, cha, am1 = 0;
System.out
.println("\n\n\n\n\nTo Add more Items:1\n\nTo View Existing Items:2");
cha = in.nextInt();
while (cha != 'q') {
if (cha == 1)
do {

System.out.println("\n\n\n\nEnter amount of:");
System.out
.println("\nFruits:1\nVegetables:2\nToys:3\nGroceries:4");
cha1 = in.nextInt();
if (cha1 == 1) {
System.out.println("Fruits=" + fruits
+ "\n\nEnter Amount:");
fruits += (am1 = in.nextInt());
System.out.println("New Amount:" + fruits);
} else if (cha1 == 2) {
System.out.println("Vegetables=" + veg
+ "\n\nEnter Amount:");
veg += (am1 = in.nextInt());
System.out.println("New Amount:" + veg);
} else if (cha1 == 3) {
System.out.println("Toys=" + toy + "\n\nEnter Amount:");
toy += (am1 = in.nextInt());
System.out.println("New Amount:" + toy);
} else if (cha1 == 4) {
System.out.println("Groceries=" + groc
+ "\n\nEnter Amount:");
groc += (am1 = in.nextInt());
System.out.println("New Amount:" + groc);
} else {
System.out
.println("Wrong Input \n\n"
+ "Enter 1 To Terminate or any other digit to Continue?");
cha2 = in.nextInt();
if(cha2 == 1) {
cha = 'q';
}
}
} while (cha2 != 1);

}

This terminates correctly, because I set cha to 'q' if the user entered '1'

However, this is bad practice.
You shouldn't declare cha as int, if you actually save and test for characters. Use the type char.
The use of in.nextInt() will crash the program if the user enters a character. The correct approach is to read the line into a String instead.

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Re: Infinite Loop
« Reply #6 on: February 08, 2015, 09:28:59 pm »


However, this is bad practice.
You shouldn't declare cha as int, if you actually save and test for characters. Use the type char.
The use of in.nextInt() will crash the program if the user enters a character. The correct approach is to read the line into a String instead.

yup, now i get it, actually I am doing a java self study so I haven't tried different things, i will now in other classes. Thanks
« Last Edit: February 08, 2015, 09:29:14 pm by haseebr21 »
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.