This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Exception in thread "main" java.lang.RuntimeException

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently having some issues getting the last line to not show no issues before running and I have tried to remove it before running the program. I am needing to show to total annual compensation which is the combination of both the fixed salary and commission but as I am still learning Java I know there is alot that I am missing, so if there is anything that there is anything that you may see that I could add please point out and that would be helpful as I am always looking forward to learning.

Here is what I have so far.


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package control.structures1;
import java.util.Scanner;

/**
*
* @author karlyn
*/
public class ControlStructures1 {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
       double salary;
       double commissionsales;
       double total;
       System.out.println("Please enter the fixed salary");
       System.out.println("Please enter current commission sales");
       Scanner in = new Scanner(System.in);
       salary = in.nextDouble();
       total = (salary * commissionsales);
       System.out.println("Total annual compensation"+total);
       
 
Sheriff
Posts: 6269
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Karlyn Kraft!

Is that exactly what you have for the code?  Assuming you just didn't miss them with the copy/paste, you need two close braces at the end of your code.

Also, in the future, be sure to UseCodeTags (that's a link).
 
Knute Snortum
Sheriff
Posts: 6269
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you are getting an exception, please copy and paste the entire error message with the stack trace and post it here.
 
Karlyn Kraft
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By closing braces do you mean } ?

So would it show like this?



/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package control.structures1;
import java.util.Scanner;

/**
*
* @author karlyn
*/
public class ControlStructures1 {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
       double salary;
       double commissionsales;
       double total;
       System.out.println("Please enter the fixed salary");
       System.out.println("Please enter current commission sales");
       Scanner in = new Scanner(System.in);
       salary = in.nextDouble();
       total = (salary * commissionsales);
       System.out.println("Total annual compensation"+total);
   }    
}


And this is the entire message I recieve.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable commissionsales might not have been initialized
at control.structures1.ControlStructures1.main(ControlStructures1.java:26)
 
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't a real exception; that is a response from an IDE (more details here). What is the value of the uninitialised variable? Where did you give it a value?
 
Karlyn Kraft
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fixed salary is $30,000 and the commission sales is 7%. So I am just trying to figure out where I am messing up and where I can improve in order to get the total annual compensation amount at the end.
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variables that you declare within a method are local variables and are not initialised by default. You need to set the value.

 
Karlyn Kraft
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok mind if I continue to post here to have someone to look at it for possible additions? As I am still learning as you can see.
 
Karlyn Kraft
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok how about this. I ran the program and it went through, but I don't know if you may see something that I could add to it.


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package control.structures1;
import java.util.Scanner;

/**
*
* @author karlyn
*/
public class ControlStructures1 {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
       double salary;
       salary = 30000
       int commissionsales;
       commissionsales = 7
       double total;
       System.out.println("Please enter the fixed salary");
       System.out.println("Please enter current commission sales");
       Scanner in = new Scanner(System.in);
       salary = in.nextDouble();
       total = (salary * commissionsales);
       System.out.println("Total annual compensation "+total);
   }    
}
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are asking for commission from the keyboard and not reading it. Follown the execution of that program and see which input is asked for and which input is taken.
 
Knute Snortum
Sheriff
Posts: 6269
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karlyn Kraft, please use code tags when you post code.  It makes it easier to see the code, it give your code some syntax highlighting, and it displays line numbers that we can reference in our posts.
 
Knute Snortum
Sheriff
Posts: 6269
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting your code in code tags looks like this:
 
Knute Snortum
Sheriff
Posts: 6269
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I and others can reference the code when we talk about it.

* Move line 26 to 14.  Start the line with private static final.  This is because you never want to create more than one Scanner per program.
* In general, define variables just as or before you need them.  So line 19-20 could just be double salary = 30000; Same thing with lines 21-22.
* Line 20 doesn't end with a semicolon (;).
* Remove line 23 and add "double" to the beginning of line 28.
* Move line 27 to between lines 24 and 25.  When you ask for a salary is when you should get it from a scanner.  
* line 27 should be commissionsales = in.nextDouble();
* Since you are getting values for salary and commissionsales now, there is no reason to have lines 19-23.  The new line between 24 and 25 will look like this: double salary = in.nextDouble(); and line 27 will look like this: double commissionsales = in.nextDouble();
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!