Results 1 to 14 of 14
  1. #1
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Need Static variable help!

    Okay, so I'm working on an assignment that has me including Static Methods and Variables into my program. One of the specifications is that I change my "int choice" to a static. I am having trouble with that...here is my code.

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    Scanner read=new Scanner(System.in);
    public static int choice;
    private static int count=0, total=0;
    private String country;
    private double dollars, value, amount;
    NumberFormat dollarForm=NumberFormat.getCurrencyInstance();
    
    public Foreign()
    {
    choice=0;
    country="";
    dollars=0.0;
    value=0.0;
    amount=0.0;
    }
    
    public static void title()
    {
    System.out.println("Foreign Exchange calculator");
    }
    
    	public static void menu()
    	{
    	System.out.println("U.S. Dollar to Foreign Currency Exchange");
    	System.out.println("1. U.S. to Canada");
    	System.out.println("2. U.S. to Mexico");
    	System.out.println("3. U.S. to Japan");
    	System.out.println("4. U.S. to Euro");
    	System.out.println("0. Quit\n");
    	System.out.print("\nPlease enter your choice:");
    	choice=read.nextInt();
    	}
    
    public static int choice()
    {
    return choice;
    }
    
    
    		public void dollars()
    		{
    		System.out.print("\nPlease enter the amount of U.S. Dollars: ");
    		dollars= read.nextDouble();
    					
    			switch (choice)
    			{
    			case 1:
    				value = 1.1553;
    				country = "Canadian Dollars";
    			break;
    					
    			case 2:
    				value = 10.550;
    				country = "Mexican Peso";
    			break;
    					
    			case 3:
    				value = 117.57;
    				country = "Japanese Yen";
    			break;
    					
    			case 4:
    				value = .8407;
    				country = "Euro";
    			break;
    			}
    			count++;	
    		}
    			
    public void amount()
    {
    amount=value*dollars;
    total+=dollars;
    }
    	
    public void vertical()
    {
    System.out.println("Country = " +country);
    System.out.println("Rate = " +dollarForm.format(value));
    System.out.println("Dollars = " +dollarForm.format(dollars));
    System.out.println("Converted value = " +dollarForm.format(amount));
    }
    
    public static void counters()
    {
    System.out.println("\n The total transaction(s): " +count);
    System.out.println("The total conversation amount: " +dollarForm.format(total));
    }
    
    public String toString()
    {
    		String horizontal;
    		horizontal = country + " " + value + " " + dollars + " " + amount;
    		return horizontal;
    }
    }
    Java Code:
    public class exchange
    {
    	public static void main(String[] args)
    	{
    		Foreign exchange;
    		int choice
    		
    		do
    		{
    			Foreign.title();
    			Foreign.menu();
    			choice= Foreign.getchoice();
    			exchange=new Foreign();
    			
    			if (choice>= 1 && choice<= 4)
    			{
    			exchange.dollars();
    			exchange.amount();
    			exchange.vertical();
    			System.out.println("\n" +exchange);
    			System.out.println("\n");
    			}
    					else if (choice > 4)
    					{
    					System.out.println("Please select 1 through 4, or 0 to quit");
    					}
    		}
    		while (choice != 0);
    		
    		Foreign.counter();
    	}
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    what trouble are you having with it? once you write 'static' it becomes static.

    Java Code:
    public static int choice()
    {
    return choice;
    }

  3. #3
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    [Foreign.java:41: non-static variable read cannot be referenced from a static context
    choice=read.nextInt();

    That's the error I get when trying to compile.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you should read Scanner.nextInt() into a temporary variable
    Java Code:
    int tempChoice = read.nextInt();

    then set the static variable
    Java Code:
    choice = tempChoice

  5. #5
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    I get the same error code. But I'm unfamiliar with Temporary Variables. I'm sure I didn't do everything required...elaborate?

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    temporary as in you dont store that variable, you just use it to hold a value temporarily and then use it to hold the next value until you dont need to use it. dont worry about it. i'm just going to copy your code, try to compile it and see what i can do...

  7. #7
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    I fixed the code by bringing the scanner within the class. But now, how do I utilize the static choice? Well, I added a getchoice() class, but the issue now is that it doesn't seem to pull my int into my switch conditional. So, the conversion process never happens. I just get 0s.

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    Scanner read=new Scanner(System.in);
    public static int choice;
    private static int count=0, total=0;
    private String country;
    private double dollars, value, amount;
    NumberFormat dollarForm=NumberFormat.getCurrencyInstance();
    
    public Foreign()
    {
    choice=0;
    country="";
    dollars=0.0;
    value=0.0;
    amount=0.0;
    }
    
    public static void title()
    {
    System.out.println("Foreign Exchange calculator");
    }
    
    	public static void menu()
    	{
    	Scanner read=new Scanner(System.in);
    	System.out.println("U.S. Dollar to Foreign Currency Exchange");
    	System.out.println("1. U.S. to Canada");
    	System.out.println("2. U.S. to Mexico");
    	System.out.println("3. U.S. to Japan");
    	System.out.println("4. U.S. to Euro");
    	System.out.println("0. Quit\n");
    	System.out.print("\nPlease enter your choice:");
    	choice=read.nextInt();
    	}
    
    public static int getchoice()
    {
    return choice;
    }
    
    		public void dollars()
    		{
    		System.out.print("\nPlease enter the amount of U.S. Dollars: ");
    		dollars= read.nextDouble();
    					
    			switch (choice)
    			{
    			case 1:
    				value = 1.1553;
    				country = "Canadian Dollars";
    			break;
    					
    			case 2:
    				value = 10.550;
    				country = "Mexican Peso";
    			break;
    					
    			case 3:
    				value = 117.57;
    				country = "Japanese Yen";
    			break;
    					
    			case 4:
    				value = .8407;
    				country = "Euro";
    			break;
    			}
    			count++;	
    		}
    			
    public void amount()
    {
    amount=value*dollars;
    }
    	
    public void vertical()
    {
    System.out.println("Country = " +country);
    System.out.println("Rate = " +dollarForm.format(value));
    System.out.println("Dollars = " +dollarForm.format(dollars));
    System.out.println("Converted value = " +dollarForm.format(amount));
    }
    
    public static void counter()
    {
    System.out.println("\n The total transaction(s): " +count);
    }
    
    public String toString()
    {
    		String horizontal;
    		horizontal = country + " " + value + " " + dollars + " " + amount;
    		return horizontal;
    }
    }
    Java Code:
    public class lab6
    {
    	public static void main(String[] args)
    	{
    		Foreign exchange;
    		int choice;
    		
    		do
    		{
    			Foreign.title();
    			Foreign.menu();
    			choice= Foreign.getchoice(); //old code for how I used to get my int choice.
    			exchange=new Foreign();
    			
    			if (choice>= 1 && choice<= 4)
    			{
    			exchange.dollars();
    			exchange.amount();
    			exchange.vertical();
    			System.out.println("\n" +exchange);
    			System.out.println("\n");
    			}
    					else if (choice > 4)
    					{
    					System.out.println("Please select 1 through 4, or 0 to quit");
    					}
    		}
    		while (choice != 0);
    		
    		Foreign.counter();
    	}
    }
    Last edited by Gaebril; 03-25-2011 at 11:39 PM.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    both classes are now compiling with these changes:

    Java Code:
    semi-colon after variable declaration
    int choice ---> int choice;
    
    wrong method name
    Foreign.getchoice() ---> changed method 'choice()' in class Foreign to 'getchoice()'
    
    wrong method name
    Foreign.counter() ---> Foreign.counters()
    
    non-static variable called in static context
    NumberFormat dollarForm ---> static NumberFormat dollarForm
    
    non-static variable read from static context
    Scanner read=new Scanner(System.in); ---> static Scanner read=new Scanner(System.in);

    It is apparent that using a variable called "read" is highly confusing when reading error messages from the system, all the time the system was saying variable READ is non-static and i didnt notice it until the 10th time i read it
    Last edited by ozzyman; 03-25-2011 at 11:46 PM.

  9. #9
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    I'm constantly switching my code trying different variations, which is why there was some errors there. I changed those. My issue now is that the int choice isn't "working". When I run the program, and select option 2 (arbitrary), input my random amount of money, the results come out as 0s and no country is selected.

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    take out all the preset values i.e. delete this:
    Java Code:
    public Foreign()
    {
    choice=0;
    country="";
    dollars=0.0;
    value=0.0;
    amount=0.0;
    }
    its completely unneccessary, i did this and it works:

    Java Code:
    public Foreign()
    {
    }

  11. #11
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Yea that seemed to work fine. But in the interest of education, is there a point to making the my "int choice" static?

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    no this was the problem:
    non-static variable read cannot be referenced from a static context
    choice=read.nextInt();

    when the human brain sees a word that appears to be a double or slightly out of place, it misses it out. reading it several times makes you realise that it actually says:
    non-static variable "READ" cannot be referenced from a static context

    i think what i saw the first few times was this:
    non-static variable cannot be read/referenced from a static context

  13. #13
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Haha, yea I picked up on that. I was just curious about the reasoning behind making my integer choice a static variable. It didn't seem to make the code any easier to write, or remove extraneous code. All I did was add static to it...

    - In regards to my assignment. Wondering if there is an Advantage to making my integer to static. Didn't seem to change the code much.
    Last edited by Gaebril; 03-26-2011 at 12:24 AM.

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    the reasoning was that, i read 'choice' to be the variable the compiler was complaining about. in fact it was complaining about 'read' so yeah

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2010, 07:43 AM
  2. Replies: 5
    Last Post: 10-15-2010, 05:21 AM
  3. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  4. About static variable
    By MarkWilson in forum New To Java
    Replies: 5
    Last Post: 06-27-2008, 02:43 PM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •