Results 1 to 8 of 8
- 02-21-2015, 11:10 PM #1
Member
- Join Date
- Feb 2015
- Posts
- 19
- Rep Power
- 0
Getting an error " variable may not have been initialized'
Hey guys, I know what the error means but I don't think initializing the variable will make my code work as intended so I'm having a little dilemna here... here's the code and I'll highlight the part that is said to be not initialized:
Java Code:import java.util.Scanner; public class ItemCost { public static void main (String []args){ int i=1,item=1,e=1, f=1, g=1; int items, d ; double gst, qst, subt, Tot = 1, PriceItems ; Scanner x = new Scanner(System.in); Scanner y = new Scanner(System.in); Scanner z = new Scanner(System.in); Scanner w = new Scanner(System.in); System.out.println("Please input the amount of items bought"); items = x.nextInt(); while ( items < 1 || items > 10) { System.out.print("Sorry the input was not correct, please try again\n"); items = x.nextInt(); i++; } do { System.out.print("Please input the price of the item "+item + " " ); PriceItems = y.nextDouble(); [B][SIZE=4] subt += PriceItems;[/SIZE][/B] if ( PriceItems < 1 || PriceItems > 1000 ) e++; else item++; }while (item <= items); System.out.println("Please input the rate of GST in %"); gst = z.nextDouble(); while (gst < 0 || gst > 22) { System.out.print("Sorry, this was incorrect, please try again"); gst = z.nextDouble(); f++; } System.out.println("Please input the rate of QST in %"); qst = w.nextDouble(); while (qst < 0 || qst > 18) { System.out.print("Sorry, this was incorrect, please try again"); qst = z.nextDouble(); g++; } } }
Java Code:Please input the amount of items bought 2 Please input the price of the item 1 1 Please input the price of the item 2 2 Please input the rate of GST in % 20 Please input the rate of QST in % 18
THanks for the help in advance guys!
- 02-22-2015, 12:01 AM #2
Re: Getting an error " variable may not have been initialized'
What is the program's current output?
Note: Single letter variable names make the code harder to read and understand. Use names that describe what the variable contains.If you don't understand my response, don't ignore it, ask a question.
- 02-22-2015, 12:09 AM #3
Member
- Join Date
- Feb 2015
- Posts
- 19
- Rep Power
- 0
Re: Getting an error " variable may not have been initialized'
Sorry, this is like my third program, trying to get use to it. I'll change that. The current output would be something like this :
Java Code:Please input the amount of items bought 2 Please input the price of the item 1 1 Please input the price of the item 2 2 Please input the rate of GST in % 10 Please input the rate of QST in % 9 2.0
- 02-22-2015, 12:13 AM #4
Re: Getting an error " variable may not have been initialized'
current output would be
What happens when you compile and execute the program?
Java coding conventions says variable names should start with a lowercase letter.If you don't understand my response, don't ignore it, ask a question.
- 02-22-2015, 02:34 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Re: Getting an error " variable may not have been initialized'
subt += PriceItems;
[Edit]
HOWEVER my program doesn't seem to add input of item 1, and 2 if I initialize subt= 0 initially. It'll only take the last value inputted in the loop.
subt does have to be initialised. I"m not sure what you mean by the program "taking" values. If you find some variable has got a strange value after the do loop, use System.out.println() to print its value then post the whole output together with what you expected the value would be.Last edited by pbrockway2; 02-22-2015 at 02:42 AM.
- 02-22-2015, 04:31 AM #6
Member
- Join Date
- Feb 2015
- Posts
- 19
- Rep Power
- 0
Re: Getting an error " variable may not have been initialized'
Java Code:Please input the amount of items bought 2 Please input the price of the item 1 1 Please input the price of the item 2 2 Please input the rate of GST in % 10 Please input the rate of QST in % 9 2.0
The answer should be the price of (in this case) Item 1 + item 2 = 3
Now, I know I don't know anything about java. I just feel that the program doesn't keep in memory the previous input of the user ( in the do-while loop where they are ask to input the price --> PriceItems). In anyway, I don't know how to remediate to that problem
EDIT**** hum.... now that I initialized it to 0 ( Eclipse "suggested it"), the program worked, any idea why?Last edited by TheRockIng; 02-22-2015 at 04:36 AM.
- 02-22-2015, 05:14 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Getting an error " variable may not have been initialized'
When you do the following:
Java Code:subt =+ PriceItems;
Java Code:subt += PriceItems;
Java Code:subt = subt + PriceItems;
previous posts).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-22-2015, 07:42 AM #8
Similar Threads
-
Getting error while creating a method "void is an invalid type for the variable"
By LearnerJ in forum New To JavaReplies: 3Last Post: 06-26-2014, 07:54 AM -
Error: The local variable city may not have been initialized
By Cinnic in forum New To JavaReplies: 4Last Post: 06-29-2013, 04:57 AM -
Need help on "variable might not have been initialized"
By CrushingJava in forum New To JavaReplies: 6Last Post: 06-28-2013, 12:35 PM -
Help needed with "Variable is not public in component" error.
By Humphrey Bogart in forum New To JavaReplies: 3Last Post: 05-29-2013, 10:27 PM -
Java 169 Error: One of my variable haven't been initialized + 3 other questions
By giga in forum New To JavaReplies: 4Last Post: 06-29-2012, 11:05 PM
Bookmarks