New Posts  All Forums:Forum Nav:

Beginner Java question....

post #1 of 24
Thread Starter 
I already fixed my problem! so you don't have to read on!









Alright everyone! New question! yay!

So I have gotten to the GUI part of the tutorial. I am working on changing all my programs from console applications to GUI ones. I started with a "Program selector", which has a series of buttons, each opening another "mini-program" that I wrote. Now, i am working on my calculator. I has it working fine before I attempted to use the GUI, but I had to rewrite it with the GUI.

FYI, I there is probablly a better way to do this, but i am a beginnner:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame{

    private JTextField input;
    public static int operator = 0;
    private static int input1;

    public Calculator() {
            super("yayay calculator!!");
setLayout(new FlowLayout());

input = new JTextField(20);
add(input);

Fieldhandler fieldhandler = new Fieldhandler();
input.addActionListener(fieldhandler);

JButton equals = new JButton("=", null);
equals.setActionCommand("equals");
add(equals);
    
JButton add = new JButton("+", null);
add.setActionCommand("add");
add(add);

JButton sub = new JButton("-", null);
add.setActionCommand("sub");
add(sub);

JButton div = new JButton("/", null);
add.setActionCommand("div");
add(div);

JButton mult = new JButton("*", null);
add.setActionCommand("mult");
add(mult);



    Buttonhandler buttonhandler = new Buttonhandler();
    add.addActionListener(buttonhandler);
    sub.addActionListener(buttonhandler);
    div.addActionListener(buttonhandler);
    mult.addActionListener(buttonhandler);
    equals.addActionListener(buttonhandler);
    }
    
    private class Buttonhandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    
    if("add".equals(e.getActionCommand())) operator = 1;   
    if("sub".equals(e.getActionCommand())) operator = 2;
    if("div".equals(e.getActionCommand())) operator = 3;
    if("mult".equals(e.getActionCommand()))operator = 4;
    if("equals".equals(e.getActionCommand())) math(input1, operator);
    }
    }    
    
    private class Fieldhandler implements ActionListener{
public void actionPerformed(ActionEvent event){

int input1 = Integer.parseInt(event.getActionCommand());


}
}
    
    public static void math(int input1, int o){
int a = 0;
int b = 0;
int r = 0;

boolean done = false;

if (done == false){
a = input1;
done = true;
}
else{
b = input1;

switch(operator){
case 1: System.out.println("add"); //r = a + b;
case 2: r = a - b;
case 3: r = a/b;
case 4: r = a*b;
}
JOptionPane.showMessageDialog(null, "the answer is" + r);
}
    }
}

I hope this is not too confusing!! thanks
Edited by Jtvd78 - 3/24/11 at 5:10pm
post #2 of 24
while(1==1) will loop and never terminate- even tho u break out of it. - not good code to have while loops like that.

reading the code now.. to see where that error is.
Edited by andrewmchugh - 3/18/11 at 5:32pm
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #3 of 24
Quote:
Originally Posted by Jtvd78 View Post
I just started java yesterday, and I tried creating a couple of programs from what I learned. I made a sucessfully made a calculator, and then i went on to make an Average calculator. It takes in input for each number, and adds 1 to the counter for every number entered. When the user has put in all of their numbers, they enter "done", and the program calculates the average of the numbers. I get several errors when running it tho.

Code:

import java.util.Scanner;
public class AvgCalc {
    
    public static void math(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter The numbers that you want to average. When you are done, type \\"done\\". ");
    String input;
    int counter=0; 
    double answer = 0, total = 0;

       
    input = scanner.nextLine();  

    while(input != "done"){
          total = total + Integer.parseInt(input);
          counter++;
          input = scanner.nextLine();  
    }     
      answer = total/counter;      
     System.out.println("The average is" + answer);
}
}
      
Netbeans gave me this:


I bet there is an obvious error that I am not catching, but What did I do wrong? ( There is more to the program, so I am just not showing that part. I do have a main method)
I made some changes in bold...keep in mind that you would be performing integer division without the above changes. Changing answer and total to doubles will make them hold decimal values.
Edited by surfbumb - 3/18/11 at 5:46pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #4 of 24
Thread Starter 
Quote:
Originally Posted by andrewmchugh View Post
while(1==1) will loop and never terminate - not good code to have while loops like that.

reading the code now.. to see where that error is.
I did that to make the loop infinite, which will let the user enter as many numbers to average as they want. When the user inputs "done" it will break, and get out of the loop. Think thats how it works
post #5 of 24
Thread Starter 
Quote:
Originally Posted by surfbumb View Post
change your while loop condition also....check the bold lines for my changes.
Read above to see why i made the while loop like that. And when I made that change, it still didn't work
post #6 of 24
Quote:
Originally Posted by Jtvd78 View Post
Read above to see why i made the while loop like that. And when I made that change, it still didn't work
if you gonna do it that way use while(true), but ud be best with a switch statement to carry on or terminate
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #7 of 24
Thread Starter 
Quote:
Originally Posted by andrewmchugh View Post
if you gonna do it that way use while(true), but ud be best with a switch statement to carry on or terminate
The thing is, you can't use a string with a switch statement.
post #8 of 24
Quote:
Originally Posted by Jtvd78 View Post
The thing is, you can't use a string with a switch statement.
LOL wat,

switch (myString) {
case "ohrly" ......
Edited by andrewmchugh - 3/18/11 at 5:49pm
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #9 of 24
Quote:
Originally Posted by Jtvd78 View Post
The thing is, you can't use a string with a switch statement.
look at my code in post# 3...should work for what you are doing.
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #10 of 24
Thread Starter 
Quote:
Originally Posted by andrewmchugh View Post
LOL wat,
Thats what NetBeans says. it says I need java 7, so I downloaded the beta, and I still get the error that I cant use a string with a switch.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming