Making dialogs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Camya
    New Member
    • Oct 2007
    • 2

    Making dialogs

    I'm trying to write a program that asks the user two numbers - the 1st number is the income, the 2nd is the tax class (either 1 or 2).

    If the tax class is 1, the program will divide the income by 10, if the tax class is 2, it will divide by 15. So if a user puts in "8000" as his income and "2" as his tax class, there will be a plain message saying "You're paying 4000 in taxes" (taxes=8000/15).

    I have no idea how to even start on this. I'm able to write a simple program that asks a user to type in his name, then shows a "Hello <NAME>!" message... but not this. Can someone please help me, give me a push in the right direction? It would be so much appreciated.

    this is what I've got so far - I can't even get the if/else thing to work. I've only done this for a couple of weeks >.<


    Code:
        public class eks_sett02_oppg4a {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		int k=1;
    		int j=2;
    		
    		
    		String s=javax.swing.JOptionPane.showInputDialog("Tast inn din inntekt");
    		System.out.println(s);
    		String t=javax.swing.JOptionPane.showInputDialog("Tast inn din skatteklasse");
    		
    		System.out.println(t);
    		
    		if (t==k){
    			
    			System.out.println("bleh");
    		}
    			
    		else if (t==j){
    			System.out.println("bah");
    		}
    		
    		
    		
    		
    		
    
    	}
    
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You're almost there (the input part that is): that JOptionPane can get a String
    from the user; you can convert that String to a number (if it is a representation
    of a number); read the API documentation for the Integer or Double classes.
    They implement methods that can convert appropriate Strings to ints or doubles.

    kind regards,

    Jos

    Comment

    • eeriehunk
      New Member
      • Sep 2007
      • 55

      #3
      Below is the code, but I dont understand how 8000/15 equals 4000. Anyway check the code below

      [ spoonfeeding code removed ]

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        @eeriehunk: please don't spoonfeed code; read the forum guidelines (see the
        'Help' link near the top right of this page).

        kind regards,

        Jos

        Comment

        • eeriehunk
          New Member
          • Sep 2007
          • 55

          #5
          Originally posted by JosAH
          @eeriehunk: please don't spoonfeed code; read the forum guidelines (see the
          'Help' link near the top right of this page).

          kind regards,

          Jos
          Hey, Jos ... Didnt realise that.. Just was trying to help and contribute.
          Regards,
          eeriehunk

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by eeriehunk
            Hey, Jos ... Didnt realise that.. Just was trying to help and contribute.
            Regards,
            eeriehunk
            No problem and no hard feelings; just don't post boilerplate code; it really doesn't
            help the OPs; most of them just copy and paste it and even turn it in as if it were
            their own code. There's too much trash in the IT world already (you wouldn't
            want a cheater as a colleague?). This is all not implying that the current OP
            would be cheating. And it also doesn't imply that your contributions are not
            appreciated; keep up the good work.

            kind regards,

            Jos

            Comment

            • thurowora
              New Member
              • Oct 2007
              • 1

              #7
              Hey hi bro nice code but i wanna u to work a few:
              Use J2SE 6(SDK i meant)
              Here i just show u how u can use ur Interger or double
              I get ur string as u use s but after da convert into int or double wid nextInt or nextDouble,rewr ite ur prog after reding mine good luck see ya soon

              [ code snipped; see next reply ]

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by thurowora
                Hey hi bro nice code but i wanna u to work a few:
                Use J2SE 6(SDK i meant)
                Here i just show u how u can use ur Interger or double
                I get ur string as u use s but after da convert into int or double wid nextInt or nextDouble,rewr ite ur prog after reding mine good luck see ya soon

                [ code snipped ]
                Please mind your spelling, please don't post spoonfeeding code and most
                important: please read the forum posting guidelines (select the 'Help' link
                near the top right corner of this page).

                kind regards,

                Jos

                Comment

                • Camya
                  New Member
                  • Oct 2007
                  • 2

                  #9
                  Thanks you so much for the help - it took me a while, but I finally figured it out ^^ Aahh... it feels good :D

                  or... I think I did. It works at least, but I don't know if I did it "right".

                  Code:
                  public class task4 {
                  
                  	/**
                  	 * @param args
                  	 */
                  	public static void main(String[] args) {
                  		// TODO Auto-generated method stub
                  		double tax;
                  		double income;
                  		int taxclass;
                  		
                  		String s=javax.swing.JOptionPane.showInputDialog("Your income is:");
                  		String t=javax.swing.JOptionPane.showInputDialog("Your tax class is:");
                  		
                  		tax class = Integer.parseInt(t);
                  		income = Double.parseDouble(s);
                  		
                  		if(taxclass ==1){
                  			tax=income/10;
                  		}
                  		else {
                  			tax=income/15;
                  		}
                  		
                  		String tittel=("Tax");
                  		String info= "Taxes are "+tax;
                  		javax.swing.JOptionPane.showMessageDialog(null, info, tittel, javax.swing.JOptionPane.PLAIN_MESSAGE);
                  
                  	}
                  
                  }
                  I've got another question.

                  Wasn't sure whether I'm supposed to make a new thread....? Anyhow, my next task is

                  "Make a program that asks the user for a text, prints out the number of characters in the text, splits the text in half and switch the parts and returns the result to the user".

                  Meaning abcdefgh should come out as "efghabcd".

                  Um. I think I can figure it out, sooner or later, except for the part about splitting the text. Not asking anyone to spoonfeed me code, but a little hint on this area would be great :)

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Camya
                    Thanks you so much for the help - it took me a while, but I finally figured it out ^^ Aahh... it feels good :D

                    or... I think I did. It works at least, but I don't know if I did it "right".

                    Code:
                    public class task4 {
                    
                    	/**
                    	 * @param args
                    	 */
                    	public static void main(String[] args) {
                    		// TODO Auto-generated method stub
                    		double tax;
                    		double income;
                    		int taxclass;
                    		
                    		String s=javax.swing.JOptionPane.showInputDialog("Your income is:");
                    		String t=javax.swing.JOptionPane.showInputDialog("Your tax class is:");
                    		
                    		tax class = Integer.parseInt(t);
                    		income = Double.parseDouble(s);
                    		
                    		if(taxclass ==1){
                    			tax=income/10;
                    		}
                    		else {
                    			tax=income/15;
                    		}
                    		
                    		String tittel=("Tax");
                    		String info= "Taxes are "+tax;
                    		javax.swing.JOptionPane.showMessageDialog(null, info, tittel, javax.swing.JOptionPane.PLAIN_MESSAGE);
                    
                    	}
                    
                    }
                    I've got another question.

                    Wasn't sure whether I'm supposed to make a new thread....? Anyhow, my next task is

                    "Make a program that asks the user for a text, prints out the number of characters in the text, splits the text in half and switch the parts and returns the result to the user".

                    Meaning abcdefgh should come out as "efghabcd".

                    Um. I think I can figure it out, sooner or later, except for the part about splitting the text. Not asking anyone to spoonfeed me code, but a little hint on this area would be great :)
                    You should probably start a new thread for it but I don't think it will be a long thread anyway. You want to have a look at the String.susbstri ng method. Open the API docs for the String class. You'll find some more usefull methods there.

                    Comment

                    Working...