LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java String variable re-assignment (https://www.linuxquestions.org/questions/programming-9/java-string-variable-re-assignment-601953/)

Cyhaxor 11-23-2007 03:00 PM

Java String variable re-assignment
 
Code:

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..

Any help would be appreciated!

ilikejam 11-23-2007 03:39 PM

Hi.

It's throwing that error, because Scanner doesn't have a nextString() method. Your syntax is otherwise correct.

http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html

Dave

testing 11-23-2007 03:57 PM

Try this:
Code:

stop = in.next();
p.s. put this line 'Scanner in = new Scanner(System.in);' outside the loop... 'cause it's time consuming.

Cyhaxor 11-23-2007 04:18 PM

Quote:

Originally Posted by testing (Post 2968730)
Try this:
Code:

stop = in.next();
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 :D Thanks anw... At the moment I am lost in JAVA API's :p

Alien_Hominid 11-23-2007 05:00 PM

Try:
Code:


while (stop == "Y")

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.

Cyhaxor 11-23-2007 07:16 PM

Quote:

Originally Posted by Alien_Hominid (Post 2968781)
Try:
Code:


while (stop == "Y")

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.

testing 11-23-2007 07:36 PM

Quote:

Originally Posted by Cyhaxor
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();
        }
    }
}

It'll work!

zhoun 11-23-2007 07:39 PM

Code:

while (stop == "Y")
in java, string compare should like this:

Code:

while ("N".equals(stop))

Cyhaxor 11-23-2007 08:01 PM

Both of the last two ways work!! Thanks a lot guys!!!

Alien_Hominid 11-24-2007 04:35 PM

I've missed that stop is String and not a char. Sorry.


All times are GMT -5. The time now is 02:33 AM.