ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
import java.util.Scanner;
public class circleArea {
public static void findTheArea(String args[]) {
double ratio = 0;
double area = 0;
String stop = "Y";
while (stop != "N") {
Scanner in = new Scanner(System.in);
System.out.println("Please give the ratio of the circle:");
ratio = in.nextDouble();
area = ratio*ratio*Math.PI;
System.out.println("The area of a circle with ratio " + ratio + " is " + area);
System.out.println("If you wish to continue press Y otherwise press N");
stop = in.nextString(); /* Here is the problem when I try to compile it I get the error message "cannot find symbol - method nextString() */
}
}
}
This is my first time that I'm trying to re-assign a string Variable in java. I have done a search but the only thing I found was about how to declare it. But my problem is how to re-assign it..
p.s. put this line 'Scanner in = new Scanner(System.in);' outside the loop... 'cause it's time consuming.
In this way though it compiles it, it doesn't recognize the input and as a result I get an infinite loop Thanks anw... At the moment I am lost in JAVA API's
N or Y might need linefeed, cause when you enter N or Y and press enter, you actually enter N or Y hex code and then 0Ah (linefeed). Check java api for it.
N or Y might need linefeed, cause when you enter N or Y and press enter, you actually enter N or Y hex code and then 0Ah (linefeed). Check java api for it.
Actually I believe that happens when you declare your variable as a char. I have declare my variable as a string. My problem is to get the Scanner method to recognize it as a string.. Like you do with int -> something.nextInt(); or double -> something.nextDouble etc.
But I really appreciate your help, all of you. I will overcome this problem with a little bit more searching within tutorials because Java API's doesn't make a lot of sense. It's too complicated.
In this way though it compiles it, it doesn't recognize the input and as a result I get an infinite loop Thanks anw... At the moment I am lost in JAVA API's
Replace this line 'while (stop != "N")' by this 'while(!stop.equals("N"))'
Now your code must looks like this:
Code:
import java.util.Scanner;
public class circleArea {
public static void findTheArea(String args[]) {
double ratio = 0;
double area = 0;
String stop = "Y";
Scanner in = new Scanner(System.in);
while (!stop.equals("N")) {
System.out.println("Please give the ratio of the circle:");
ratio = in.nextDouble();
area = ratio*ratio*Math.PI;
System.out.println("The area of a circle with ratio " + ratio + " is " + area);
System.out.println("If you wish to continue press Y otherwise press N");
stop = in.next();
}
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.