9 Replies - 1503 Views - Last Post: 09 October 2012 - 06:37 PM Rate Topic: -----

#1 abdulrehmansuleman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 01:22 AM

So I am an absolute beginner at Java. I want to know how to restart the code after the program has finished. Like I want to ask the user whether he wants to use it again. If he says no, I want the program to exit. I tried a while loop, to make the program repeat over and over. And tried adding this at the end of the while loop:
System.out.println ("Do you want to use the software again? yes or no?");
Scanner s=new Scanner(System.in);
String a=s.nextLine();
if (a=="no") {System.exit(0);}}
}}


I know that it should work. But it doesn't work. Even if I type no at the end, the program just restarts. Note that if I replace the string a with an integer a, for example by int a=s.nextInt(); if (a==1) {System.exit(0);}}}}, it works if I enter 1 at the end of the program and it quits like I want it to. So that means there's some problem with my string method. Could anyone clarify this?

This post has been edited by Atli: 09 October 2012 - 04:20 AM
Reason for edit:: Replaced the bold tags with [code] and [il].


Is This A Good Question/Topic? 0
  • +

Replies To: Restarting the code using while loop and System.exit(0)

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 01:28 AM

You shouldn't even use System.exit this early on. Do this instead:

public static void main(String args[]){
	boolean doItAgain = false;
	Scanner sc = new Scanner(System.in);
	do{
		//Whatever you want your code to do
		System.out.println("Do you want to do it again? Y or N");
		if(sc.nextLine.toLowerCase().equals("y")){
			doItAgain = true;
		} else {
			doItAgain = false;
		}
	} while(doItAgain==true);
// If doItAgain evaluates to false, program ends.
}



It would also be helpful if we can see your full code.
Was This Post Helpful? 0
  • +
  • -

#3 abdulrehmansuleman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 01:34 AM

Here's the whole code

import java.util.Scanner;
public class Addloops {
public static void main (String[]args) {
Scanner scan=new Scanner(System.in); boolean b=true;
while (B)/> {System.out.println ("How many numbers do you want to add to one another?");
int x=scan.nextInt();
int y=0; System.out.println ("Enter " + x + " numbers");
for (int i=0;i<x;i++) {
int z=scan.nextInt();
y=y+z;}
System.out.println ("Your answer is " + y);
System.out.println ("Thank you for using the software");
System.out.println ("Do you want to use the software again? yes or no?");
Scanner s=new Scanner(System.in);
String a=s.nextLine();
if (a=="no") {System.exit(0);}}
}}

Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 01:41 AM

There it is. First, the variable "B" doesn't exist on your current context, also you don't have a condition to end your while statement. Your System.exit will also work if you use .equals instead of == because you're comparing by reference, not by value.

Always remember!
String cake = "Chocolate";
if(cake=="Chocolate"){
}// comparing by reference won't work!
if(cake.equals("Chocolate")){
}// comparing by value will!


Was This Post Helpful? 2
  • +
  • -

#5 abdulrehmansuleman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 02:36 AM

If I replace == with .equals("no"), it does work and the program ends. But I don't get why it works. Doesn't == mean that if the value of string is "no" (the person types no), it should terminate the program, like I want? Or does == imply something else. Also why does == work fine with integer but gives me a problem when I attempt to use string? Actually I just want to understand my mistake, so you'll have to excuse me if I am asking a very obvious question.
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 02:50 AM

In Java, there are two common ways to compare stuff, one is by the .equals method and the other is using ==. The first one compares by value, the second one compares by reference. In Java, objects have references.

For example, we have this:
String cake = "Chocolate";


An String object is created.
Now, you're thinking this would work right?
if(cake=="Chocolate"){

}


They won't since they're referring to different object references. Now if we use .equals:
if(cake.equals("Chocolate")){

}


It compares the value of cake to "Chocolate" and we get the desired effect.

Now why does == work with ints? Well, integers are primitive types, meaning they are not objects, and thus they can be treated as literals, meaning they will always be compared with value.

I hope that clears that up. ^^
Was This Post Helpful? 2
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 02:55 AM

View PostfromTheSprawl, on 09 October 2012 - 03:20 PM, said:

In Java, there are two common ways to compare stuff, one is by the .equals method and the other is using ==. The first one compares by value, the second one compares by reference. In Java, objects have references.

For example, we have this:
String cake = "Chocolate";


An String object is created.
Now, you're thinking this would work right?
if(cake=="Chocolate"){

}


They won't since they're referring to different object references. Now if we use .equals:
if(cake.equals("Chocolate")){

}


It compares the value of cake to "Chocolate" and we get the desired effect.

Now why does == work with ints? Well, integers are primitive types, meaning they are not objects, and thus they can be treated as literals, meaning they will always be compared with value.

I hope that clears that up. ^^


Thank you fromTheSprawl, I have learned something new today, thanks to your concise explanation. :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 03:27 AM

== uses the hashcode of the object to see if they are equal
Was This Post Helpful? 2
  • +
  • -

#9 abdulrehmansuleman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 06:51 AM

fromTheSprawl, I found your explanation quite helpful. So, what I understand that integer is a primitive type of variable and all primitive types of variables are compared by value even if we use == which compares by reference. And for strings, I should use .equals().
But I am still confused as to what "reference" means in java. What is a reference? And when we say that == compares references instead of actual values, what is it actually comparing if not value?

CasiOo: Can you explain what you mean by "hashcode" of the object? And how is it different from the value of the object?
Was This Post Helpful? 0
  • +
  • -

#10 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Restarting the code using while loop and System.exit(0)

Posted 09 October 2012 - 06:37 PM

I don't even know what a hashcode is, I hope I read up on it on the Java Tutorials soon! Thanks CasiOo!

Well abdulrehmansuleman, if you try comparing other objects other than String and you invoke the toString method you'll see what I mean by reference, look at this:

We create a Cake object:
public class Cake {
	private String name;
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
}



Now we go to the bakery and do some cakes!
public class Bakery {
	public static void main(String[] args) {
		Cake cake = new Cake();
		cake.setName("Chocolate cake");
		Cake anotherCake = new Cake();
		anotherCake.setName("Strawberry cake");
}
}


Now that gives us two cake objects. We add these lines now:
System.out.println(cake.getName() +" is located at " + cake.toString());
System.out.println(anotherCake.getName() +" is located at " + anotherCake.toString());


We get this output if we run our code:
Chocolate cake is located at Cake@addbf1
Strawberry cake is located at Cake@42e816


See the last word where the class name has an @ sign and some characters? That is an object reference. Java masters, correct me if I'm wrong, but I think those references refer to the location of that object in the memory.

As you can see, they are not equal! Let us verify, just in case:
if(cake.equals(anotherCake)){
			System.out.println("This cake is like that cake using the equals method!");
		} else if (cake == anotherCake){
			System.out.println("This cake is like that cake using the == method!");
		} else {
			System.out.println("They are different cakes!");
		}


Output is:
They are different cakes!



There. ^^
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1