Results 1 to 14 of 14
Thread: Need Static variable help!
- 03-25-2011, 09:49 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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(); } }
-
what trouble are you having with it? once you write 'static' it becomes static.
Java Code:public static int choice() { return choice; }
- 03-25-2011, 10:01 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
[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.
-
you should read Scanner.nextInt() into a temporary variable
Java Code:int tempChoice = read.nextInt();
then set the static variable
Java Code:choice = tempChoice
- 03-25-2011, 10:18 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
I get the same error code. But I'm unfamiliar with Temporary Variables. I'm sure I didn't do everything required...elaborate?
-
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...
- 03-25-2011, 10:29 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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 10:39 PM.
-
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 itLast edited by ozzyman; 03-25-2011 at 10:46 PM.
- 03-25-2011, 10:50 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
-
take out all the preset values i.e. delete this:
its completely unneccessary, i did this and it works:Java Code:public Foreign() { choice=0; country=""; dollars=0.0; value=0.0; amount=0.0; }
Java Code:public Foreign() { }
- 03-25-2011, 11:07 PM #11
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Yea that seemed to work fine. But in the interest of education, is there a point to making the my "int choice" static?
-
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
- 03-25-2011, 11:17 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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-25-2011 at 11:24 PM.
-
Similar Threads
-
non-static variable cannot be referenced from static context...
By MadJack in forum New To JavaReplies: 5Last Post: 12-01-2010, 06:43 AM -
non-static variable cannot be referenced from a static context
By keo in forum New To JavaReplies: 5Last Post: 10-15-2010, 04:21 AM -
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
About static variable
By MarkWilson in forum New To JavaReplies: 5Last Post: 06-27-2008, 01:43 PM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks