473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

MathStart.Java file already created this is Math.java FYI (IF statements)

Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


import java.util.*;
public class Math
{
double d1, d2, d3, total, d4, d5, sum;
public static void output()
{
Scanner scannerObject = new Scanner(System.in);
System.out.println("Let's add three whole numbers");
choice=scannerObject.nextInt();
if(choice==1)
{
Math.addThreeWhole();
}
if (choice==2)
{
Math.addTwoDecimals();
}
else if ((choice<1)||(choice>2))
{
System.out.print("You must enter either 1 or 2.\n");
System.out.print("You will need to run the program again.\n\n");
}
}
public static void addThreeWhole()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter whole number:");
d1 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d2 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d3 = scannerObject.nextDouble();
System.out.print("\n");
total = d1+d2+d3;
}
public static void addTwoDecimals()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter decimal point number:");
d4 = scannerObject.nextDouble();
System.out.print("Enter decimal point number:");
d5 = scannerObject.nextDouble();
sum = d4+d5;
}
}
Mar 28 '07 #1
7 1737
Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


import java.util.*;
public class Math
{
double d1, d2, d3, total, d4, d5, sum;
public static void output()
{
Scanner scannerObject = new Scanner(System.in);
System.out.println("Let's add three whole numbers");
choice=scannerObject.nextInt();
if(choice==1)
{
Math.addThreeWhole();
}
if (choice==2)
{
Math.addTwoDecimals();
}
else if ((choice<1)||(choice>2))
{
System.out.print("You must enter either 1 or 2.\n");
System.out.print("You will need to run the program again.\n\n");
}
}
public static void addThreeWhole()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter whole number:");
d1 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d2 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d3 = scannerObject.nextDouble();
System.out.print("\n");
total = d1+d2+d3;
}
public static void addTwoDecimals()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter decimal point number:");
d4 = scannerObject.nextDouble();
System.out.print("Enter decimal point number:");
d5 = scannerObject.nextDouble();
sum = d4+d5;
}
}
Mar 28 '07 #2
dmjpro
2,476 2GB
where u define choice.....

listen one more thing ur static method can access only non-static data members

welcome ..... have a good day
Mar 28 '07 #3
r035198x
13,262 8TB
Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


import java.util.*;
public class Math
{
double d1, d2, d3, total, d4, d5, sum;
public static void output()
{
Scanner scannerObject = new Scanner(System.in);
System.out.println("Let's add three whole numbers");
choice=scannerObject.nextInt();
if(choice==1)
{
Math.addThreeWhole();
}
if (choice==2)
{
Math.addTwoDecimals();
}
else if ((choice<1)||(choice>2))
{
System.out.print("You must enter either 1 or 2.\n");
System.out.print("You will need to run the program again.\n\n");
}
}
public static void addThreeWhole()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter whole number:");
d1 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d2 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d3 = scannerObject.nextDouble();
System.out.print("\n");
total = d1+d2+d3;
}
public static void addTwoDecimals()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter decimal point number:");
d4 = scannerObject.nextDouble();
System.out.print("Enter decimal point number:");
d5 = scannerObject.nextDouble();
sum = d4+d5;
}
}
Please use code tags next time when posting code.
Where did you tell the compiler that choice is an int?
Mar 28 '07 #4
r035198x
13,262 8TB
Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


import java.util.*;
public class Math
{
double d1, d2, d3, total, d4, d5, sum;
public static void output()
{
Scanner scannerObject = new Scanner(System.in);
System.out.println("Let's add three whole numbers");
choice=scannerObject.nextInt();
if(choice==1)
{
Math.addThreeWhole();
}
if (choice==2)
{
Math.addTwoDecimals();
}
else if ((choice<1)||(choice>2))
{
System.out.print("You must enter either 1 or 2.\n");
System.out.print("You will need to run the program again.\n\n");
}
}
public static void addThreeWhole()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter whole number:");
d1 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d2 = scannerObject.nextDouble();
System.out.print("Enter whole number:");
d3 = scannerObject.nextDouble();
System.out.print("\n");
total = d1+d2+d3;
}
public static void addTwoDecimals()
{
Scanner scannerObject = new Scanner(System.in);
System.out.print("Enter decimal point number:");
d4 = scannerObject.nextDouble();
System.out.print("Enter decimal point number:");
d5 = scannerObject.nextDouble();
sum = d4+d5;
}
}
Keep one problem in one thread please.
Mar 28 '07 #5
where u define choice.....

listen one more thing ur static method can access only non-static data members

welcome ..... have a good day
Expand|Select|Wrap|Line Numbers
  1.     import java.util.*;
  2. public class Math
  3. {
  4.  
  5.             public static double addThreeWhole =0;
  6.  
  7.             public static double addTwoDecimals =0;
  8.  
  9. public static double choice =0;
  10.  
  11. double d1, d2, d3, total;
  12.     public static void output()
  13.  
  14.     {
  15.  
  16.  
  17.         Scanner scannerObject = new Scanner(System.in);
  18.         System.out.println("Let's add three whole numbers");
  19.         choice=scannerObject.nextDouble();
  20.         if(choice==1)
  21.         {
  22.             Math.addThreeWhole();
  23.         }
  24.         if (choice==2)
  25.         {
  26.             Math.addTwoDecimals();
  27.         }
  28.         else if ((choice<1)||(choice>2))
  29.         {
  30.             System.out.print("You must enter either 1 or 2.\n");
  31.             System.out.print("You will need to run the program again.\n\n");
  32.         }
  33.     }
  34.     public static void addThreeWhole()
  35.     {
  36.         Scanner scannerObject = new Scanner(System.in);
  37.         System.out.print("Enter whole number:");
  38.         d1 = scannerObject.nextDouble();
  39.         System.out.print("Enter whole number:");
  40.         d2 = scannerObject.nextDouble();
  41.         System.out.print("Enter whole number:");
  42.         d3 = scannerObject.nextDouble();
  43.         System.out.print("\n");
  44.         total = d1+d2+d3;
  45.     }
  46.     public static void addTwoDecimals()
  47.     {
  48. double d4, d5, sum;
  49.         Scanner scannerObject = new Scanner(System.in);
  50.         System.out.print("Enter decimal point number:");
  51.         d4 = scannerObject.nextDouble();
  52.         System.out.print("Enter decimal point number:");
  53.         d5 = scannerObject.nextDouble();
  54.         sum = d4+d5;
  55.     }
  56. }
  57.  
  58.  


This is my new version. It is telling me now that non static variable cannot be referenced to a static context?? What variable is that? What do I need to change? I'm lost? I've updated this though from the Choice.
Mar 28 '07 #6
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1.     import java.util.*;
  2. public class Math
  3. {
  4.  
  5.             public static double addThreeWhole =0;
  6.  
  7.             public static double addTwoDecimals =0;
  8.  
  9. public static double choice =0;
  10.  
  11. double d1, d2, d3, total;
  12.     public static void output()
  13.  
  14.     {
  15.  
  16.  
  17.         Scanner scannerObject = new Scanner(System.in);
  18.         System.out.println("Let's add three whole numbers");
  19.         choice=scannerObject.nextDouble();
  20.         if(choice==1)
  21.         {
  22.             Math.addThreeWhole();
  23.         }
  24.         if (choice==2)
  25.         {
  26.             Math.addTwoDecimals();
  27.         }
  28.         else if ((choice<1)||(choice>2))
  29.         {
  30.             System.out.print("You must enter either 1 or 2.\n");
  31.             System.out.print("You will need to run the program again.\n\n");
  32.         }
  33.     }
  34.     public static void addThreeWhole()
  35.     {
  36.         Scanner scannerObject = new Scanner(System.in);
  37.         System.out.print("Enter whole number:");
  38.         d1 = scannerObject.nextDouble();
  39.         System.out.print("Enter whole number:");
  40.         d2 = scannerObject.nextDouble();
  41.         System.out.print("Enter whole number:");
  42.         d3 = scannerObject.nextDouble();
  43.         System.out.print("\n");
  44.         total = d1+d2+d3;
  45.     }
  46.     public static void addTwoDecimals()
  47.     {
  48. double d4, d5, sum;
  49.         Scanner scannerObject = new Scanner(System.in);
  50.         System.out.print("Enter decimal point number:");
  51.         d4 = scannerObject.nextDouble();
  52.         System.out.print("Enter decimal point number:");
  53.         d5 = scannerObject.nextDouble();
  54.         sum = d4+d5;
  55.     }
  56. }
  57.  
  58.  


This is my new version. It is telling me now that non static variable cannot be referenced to a static context?? What variable is that? What do I need to change? I'm lost? I've updated this though from the Choice.
1.)Avoid naming classes with names of classes in the java API (especially from the java.lang package)
variables d1,d2, etc must be declared static as well if you want to access them in static methods.
Mar 28 '07 #7
Thanks, I have it now.
Mar 29 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rajesh | last post by:
Hi, I am using iplanet webserver 4.1. I want to call a java class from ssjs file. But I am not getting the result. I have created a java class file and put it in the folder...
4
by: acemann7 | last post by:
Windows 2000 udb v 7.2 Got the latest fixpak 13 Applied it. Still can't get a single java sp to run. I used to get reason "2"... now reason "0" - reason 0 is not even documented. I point...
4
by: Miro | last post by:
I've read a couple of threads on this error but none of them could help me so I'm posting myself. When I try open and read (not update) an ACCESS database on a network share I occasionally get:...
0
by: satish mullapudi | last post by:
Hi, I am using DB2 v 8.2 & jdk 1.5. I have created a java "Hello World" program. Now I want to bind that program to SAMPLE database. anybody plz help me out with the detailed process to do this....
1
by: =?Utf-8?B?S2Vubnk=?= | last post by:
I have one bat file that contains a command to startup Java Program. Then, I would like to create a cluster job to call the bat file. In case of one computer is down, another computer can also call...
3
by: Begreen | last post by:
Hi All, I wrote a java program which outputs a xml file! But I would prefer this program to insert the DTD code on the fly, in the xml file when created! I want the xml file to look like...
2
eyeofsoul
by: eyeofsoul | last post by:
hello..i am having a difficulties in calling the function in other file. for example..this is the file that use xxx.java; package netbean5test; import netbean5test.xxx; import java.awt.*;...
2
by: jpr | last post by:
I have 2 classes saved as 2 different java files.(eg- class ABC.java & PQR.java) ->ABC.java classABC extends JTextField { ABC(int i) { super(i); ...
1
by: bsonline | last post by:
I have to modify a .java file in a project. But I dont have any editor to compile java. I use putty to connect server(Solaris). And using putty console I modify .java file and save it. Then I down...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.