LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2007, 02:00 PM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
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!
 
Old 11-23-2007, 02:39 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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
 
Old 11-23-2007, 02:57 PM   #3
testing
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 2
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.
 
Old 11-23-2007, 03:18 PM   #4
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by testing View Post
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 Thanks anw... At the moment I am lost in JAVA API's
 
Old 11-23-2007, 04:00 PM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
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.
 
Old 11-23-2007, 06:16 PM   #6
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Alien_Hominid View Post
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.

Last edited by Cyhaxor; 11-23-2007 at 06:17 PM.
 
Old 11-23-2007, 06:36 PM   #7
testing
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 2
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!

Last edited by testing; 11-23-2007 at 06:43 PM.
 
Old 11-23-2007, 06:39 PM   #8
zhoun
Member
 
Registered: Oct 2007
Location: anywhere
Distribution: slackware64 current & win7 64 on thinkpad X61
Posts: 104

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

Code:
while ("N".equals(stop))
 
Old 11-23-2007, 07:01 PM   #9
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Both of the last two ways work!! Thanks a lot guys!!!
 
Old 11-24-2007, 03:35 PM   #10
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
I've missed that stop is String and not a char. Sorry.
 
  


Reply

Tags
java, string



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about immediate and deferred variable assignment in GNU make George2 Programming 1 07-22-2011 05:55 PM
Perl variable string search ugenn Programming 1 05-07-2004 08:19 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
variable to string x2000koh Programming 4 07-30-2003 02:23 AM
Getting a variable name based on a string. jtshaw Programming 7 10-08-2002 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration