Calling all Java/ Comp Sci Guru's - Overclock.net - An Overclocking Community

Forum Jump: 

Calling all Java/ Comp Sci Guru's

 
Thread Tools
post #1 of 8 (permalink) Old 10-07-2009, 08:28 AM - Thread Starter
New to Overclock.net
 
Join Date: Dec 2007
Location: Dallas, Texas
Posts: 418
Rep: 10 (Unique: 10)
So I have no errors, but when I try to run after building this code, It does nothing? Any reason why? I am using netbeans to write my code, I know this is real basic but its my assignment. This is my code, +reps for help!


package jamesmills;

import javax.swing.JOptionPane;
import java.util.Scanner;


public class jamesmills
{


public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int Choice;
float Num1, Num2;

//numbers
JOptionPane.showInputDialog (null, "Enter your first Number");
Num1=input.nextFloat();
JOptionPane.showInputDialog (null, "Enter your second Number");
Num2=input.nextFloat();
//choice
JOptionPane.showInputDialog (null, "1.Addtion\
2.Subtraction\
.Multiplication\
.Division\
5.Quit");
Choice=input.nextInt();

//decision
while (Choice!=5)
{
switch (Choice)
{
case 1: addNumbers(Num1,Num2);
break;
case 2: subNumbers(Num1,Num2);
break;
case 3: multiNumbers(Num1,Num2);
break;
case 4: divideNumbers(Num1,Num2);
break;
case 5: break;
}//end switch
}//end while


}//end void main

//methods for math
public static float addNumbers(float Num1, float Num2)
{
float Total;
Total=Num1+Num2;
return Total;
}

public static float subNumbers(float Num1, float Num2)
{
float Total;
Total=Num1-Num2;
return Total;
}

public static float multiNumbers(float Num1, float Num2)
{
float Total;
Total=Num1*Num2;
return Total;
}

public static float divideNumbers(float Num1, float Num2)
{
float Total;
Total=Num1/Num2;
return Total;
}

}//end class
-bl4ck- is offline  
Sponsored Links
Advertisement
 
post #2 of 8 (permalink) Old 10-07-2009, 10:12 AM
New to Overclock.net
 
pippolo's Avatar
 
Join Date: Jun 2009
Location: Italy
Posts: 389
Rep: 94 (Unique: 82)
The Scanner reads from the standard input (and waits that you write something into the console), but the text entered into the JOptionPane isn't connected to the standard input.
The function
Code:
JOptionPane.showInputDialog (null, "Enter your first Number");
returns the entered text as a String. You have to write a thing like this:

Code:
String result = JOptionPane.showInputDialog (null, "Enter your first Number");
and after:

Code:
int number = Integer.parseInt(result);
or

Code:
Scanner scanner = new Scanner(result);
int number = scanner.nextInt();

pippolo is offline  
post #3 of 8 (permalink) Old 10-07-2009, 12:02 PM
New to Overclock.net
 
Lumas's Avatar
 
Join Date: Oct 2008
Location: US, Arizona
Posts: 70
Rep: 6 (Unique: 6)
And as a couple of syntax things:

Division has an extra space and is not numbered
Multiplication is not numbered

I believe it is also good practice when you declare your variables to set them equal to 0.

It is a mistake to assume that all men want to be free. On the contrary, if freedom entails responsibility, many of them want none of it. They would happily exchange their liberty for a modest (if illusory) security. Even those who claim to cherish their freedom are rather less enthusiastic about taking the consequences of their actions. The aim of untold millions is to be free to do exactly as they choose and for someone else to pay for when things go wrong.

Lumas is offline  
Sponsored Links
Advertisement
 
post #4 of 8 (permalink) Old 10-07-2009, 12:54 PM - Thread Starter
New to Overclock.net
 
Join Date: Dec 2007
Location: Dallas, Texas
Posts: 418
Rep: 10 (Unique: 10)
great got it working thanks
-bl4ck- is offline  
post #5 of 8 (permalink) Old 10-08-2009, 10:52 AM
New to Overclock.net
 
Black Magix's Avatar
 
Join Date: Mar 2008
Location: I'll let you decide.
Posts: 4,691
Rep: 344 (Unique: 269)
I really wish we could preserve spacing on the forums....that was hard as hell to read >.<


Black Magix is offline  
post #6 of 8 (permalink) Old 10-08-2009, 10:55 AM
New to Overclock.net
 
Join Date: Sep 2007
Location: Pennsylvania
Posts: 4,159
Rep: 261 (Unique: 206)
Quote:
Originally Posted by Black Magix View Post
I really wish we could preserve spacing on the forums....that was hard as hell to read >.<
You CAN if you wrap it in [ CODE] blocks

Code:
lsdkjf
            lsdkjf



hometoast is offline  
post #7 of 8 (permalink) Old 10-08-2009, 09:40 PM
 
godsgift2dagame's Avatar
 
Join Date: Apr 2009
Posts: 1,453
Hey dude, click "QUOTE" on my post and take everything from [quote] on down and edit your post. That was truly horrendous on the eyes.

Quote:
Originally Posted by -bl4ck- View Post
package jamesmills;

import javax.swing.JOptionPane;
import java.util.Scanner;


public class jamesmills
{


public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int Choice;
float Num1, Num2;

//numbers
JOptionPane.showInputDialog (null, "Enter your first Number");
Num1=input.nextFloat();
JOptionPane.showInputDialog (null, "Enter your second Number");
Num2=input.nextFloat();
//choice
JOptionPane.showInputDialog (null, "1.Addtion\
2.Subtraction\
.Multiplication\
.Division\
5.Quit");
Choice=input.nextInt();

//decision
while (Choice!=5)
{
switch (Choice)
{
case 1: addNumbers(Num1,Num2);
break;
case 2: subNumbers(Num1,Num2);
break;
case 3: multiNumbers(Num1,Num2);
break;
case 4: divideNumbers(Num1,Num2);
break;
case 5: break;
}//end switch
}//end while


}//end void main

//methods for math
public static float addNumbers(float Num1, float Num2)
{
float Total;
Total=Num1+Num2;
return Total;
}

public static float subNumbers(float Num1, float Num2)
{
float Total;
Total=Num1-Num2;
return Total;
}

public static float multiNumbers(float Num1, float Num2)
{
float Total;
Total=Num1*Num2;
return Total;
}

public static float divideNumbers(float Num1, float Num2)
{
float Total;
Total=Num1/Num2;
return Total;
}

}//end class
godsgift2dagame is offline  
post #8 of 8 (permalink) Old 10-08-2009, 09:56 PM
New to Overclock.net
 
Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,282
Rep: 643 (Unique: 391)
I think this is what you meant
Code:
package jamesmills;

import javax.swing.JOptionPane;
import java.util.Scanner;


public class jamesmills
{


    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int Choice;
        float Num1, Num2;

        //numbers
        JOptionPane.showInputDialog (null, "Enter your first Number");
        Num1=input.nextFloat();
        JOptionPane.showInputDialog (null, "Enter your second Number");
        Num2=input.nextFloat();
        //choice
        JOptionPane.showInputDialog (null, "1.Addtion\
2.Subtraction\
.Multiplication\
.Divis ion\
5.Quit");
        Choice=input.nextInt();

        //decision
        while (Choice!=5)
        {
            switch (Choice)
            {
                case 1: addNumbers(Num1,Num2);
                        break;
                case 2: subNumbers(Num1,Num2);
                        break;
                case 3: multiNumbers(Num1,Num2);
                        break;
                case 4: divideNumbers(Num1,Num2);
                        break;
                case 5: break;
            }//end switch
        }//end while


    }//end void main

    //methods for math
    public static float addNumbers(float Num1, float Num2)
    {
        float Total;
        Total=Num1+Num2;
        return Total;
    }

    public static float subNumbers(float Num1, float Num2)
    {
        float Total;
        Total=Num1-Num2;
        return Total;
    }

    public static float multiNumbers(float Num1, float Num2)
    {
        float Total;
        Total=Num1*Num2;
        return Total;
    }

    public static float divideNumbers(float Num1, float Num2)
    {
        float Total;
        Total=Num1/Num2;
        return Total;
    }

}//end class

rabidgnome229 is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off