Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Need to input algorithems with String values.

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Need to input algorithems with String values.

    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Random;
     
    public class SimpleWindow extends JFrame {
     
            JLabel one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,twentyone;
            JButton firstButton,secondButton;
            JTextField
    firstTextField,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,tweleth,thirteenth,fourteenth;
            JTextArea firstTextArea;
            JCheckBox firstCheckBox;
            JComboBox firstComboBox,secondComboBox,thirdComboBox,fourthComboBox;
            int d,e,f,g,h,i,j,k,l,m,n;
            int b = 25;
            int c = 10;
            String Damage;
     
            Random rand = new Random();
            int a = rand.nextInt(100) + 1;
            public SimpleWindow() {
     
     
                    createInterface();
            }
     
            void createInterface() {
            setLayout(null);
            one = new JLabel();
            two = new JLabel();
            three = new JLabel();
            four = new JLabel();
            five = new JLabel();
            six = new JLabel();
            seven = new JLabel();
            eight = new JLabel();
            nine = new JLabel();
            ten = new JLabel();
            eleven = new JLabel();
            twelve = new JLabel();
            thirteen = new JLabel();
            fourteen = new JLabel();
            fifteen=new JLabel();
            sixteen = new JLabel();
            seventeen= new JLabel();
            eighteen = new JLabel();
            nineteen = new JLabel();
            twenty = new JLabel();
            twentyone = new JLabel();
     
     
     
     
            one.setText("Combat ");
            two.setText("Armor");
            three.setText("Speed");
            four.setText("Hit Points Maximum");
            five.setText ("Critical Hit Threshold");
            six.setText ("Critical Hit Penalty");
            seven.setText("Critical Speed Penalty");
            eight.setText("Combat ");
            nine.setText("Armor");
            ten.setText("Speed");
            eleven.setText("Hit Points Maximum");
            twelve.setText("Critical Hit Threshold");
            thirteen.setText ("Critical Hit Penalty");
            fourteen.setText ("Critical Speed Penalty");
            fifteen.setText("Unit Type");
            sixteen.setText("Unit Type");
            seventeen.setText("Species");
            eighteen.setText("Species");
            nineteen.setText("Combatant 1");
            twenty.setText("Combatant 2");
            twentyone.setText("Play by Play");
     
            one.setBounds (50,170,80,20);
            two.setBounds (50,190,80,20);
            three.setBounds (50,210,80,20);
            four.setBounds (50,230,80,20);
            five.setBounds (50,250,80,20);
            six.setBounds (50,270,80,20);
            seven.setBounds (50,290,80,20);
            eight.setBounds (500,170,80,20);
            nine.setBounds (500,190,80,20);
            ten.setBounds (500,210,80,20);
            eleven.setBounds (500,230,80,20);
            twelve.setBounds (500,250,80,20);
            thirteen.setBounds (500,270,80,20);
            fourteen.setBounds (500,290,80,20);
            fifteen.setBounds (50,100,80,20);
            sixteen.setBounds (500,100,80,20);
            seventeen.setBounds(50,130,80,20);
            eighteen.setBounds(500,130,80,20);
            nineteen.setBounds(150,65,80,20);
            twenty.setBounds(600,65,80,20);
            twentyone.setBounds(325,370,82,20);
     
     
            add(one);
            add(two);
            add(three);
            add(four);
            add(five);
            add(six);
            add(seven);
            add(eight);
            add(nine);
            add(ten);
            add(eleven);
            add(twelve);
            add(thirteen);
            add(fourteen);
            add(fifteen);
            add(sixteen);
            add(seventeen);
            add(eighteen);
            add(nineteen);
            add(twenty);
            add(twentyone);
     
     
     
            firstTextField = new JTextField();
            second= new JTextField();
            third= new JTextField();
            fourth= new JTextField();
            fifth= new JTextField();
            sixth= new JTextField();
            seventh= new JTextField();
            eighth= new JTextField();
            ninth= new JTextField();
            tenth= new JTextField();
            eleventh= new JTextField();
            tweleth= new JTextField();
            thirteenth= new JTextField();
            fourteenth= new JTextField();
     
     
     
            firstTextField.setBounds(150,170,40,20);
            second.setBounds(150,190,40,20);
            third.setBounds(150,210,40,20);
            fourth.setBounds(150,230,40,20);
            fifth.setBounds(150,250,40,20);
            sixth.setBounds(150,270,40,20);
            seventh.setBounds(150,290,40,20);
            eighth.setBounds(600,170,40,20);
            ninth.setBounds(600,190,40,20);
            tenth.setBounds(600,210,40,20);
            eleventh.setBounds(600,230,40,20);
            tweleth.setBounds(600,250,40,20);
            thirteenth.setBounds(600,270,40,20);
            fourteenth.setBounds(600,290,40,20);
     
     
     
     
            add(firstTextField);
            add(second);
            add(third);
            add(fourth);
            add(fifth);
            add(sixth);
            add(seventh);
            add(eighth);
            add(ninth);
            add(tenth);
            add(eleventh);
            add(tweleth);
            add(thirteenth);
            add(fourteenth);
     
     
     
     
            firstTextArea = new JTextArea();
            firstTextArea.setText("");
            firstTextArea.setBounds(10,400,760,150 );
            add(firstTextArea);
     
            firstCheckBox= new JCheckBox();
            firstCheckBox.setText("Save output to file");
            firstCheckBox.setBounds(10,550,140,20);
            add (firstCheckBox);
     
            {firstButton = new JButton();
            secondButton= new JButton();
            firstButton.setText("Go");
            firstButton.addActionListener(new ActionListener() {
                    public void actionPerformed (ActionEvent e)     {
                            if (firstButton.getText().equals("Go")){
                                    firstButton.setText("Go");
     
                                    firstTextField.setText("25");
                                    second.setText("10");
                                    third.setText("5");
                                    fourth.setText("100");
                                    fifth.setText("50");
                                    sixth.setText("0");
                                    seventh.setText("0");
                                    eighth.setText("25");
                                    ninth.setText("10");
                                    tenth.setText("5");
                                    eleventh.setText("100");
                                    tweleth.setText("50");
                                    thirteenth.setText("0");
                                    fourteenth.setText("0");
     
                                    if(c < a)
                                    hits;
                                else no hit;
     
     
                                 Damage = b*a/100-
     
     
                                    }
                            else {
                                    firstButton.setText("Go");
                            }
                            }
                            }
                            );
            }
     
     
            secondButton.setText("Halt");
            firstButton.setBounds(650,550,75,20);
            secondButton.setBounds(550,550,75,20);
            add(firstButton);
            add(secondButton);
     
            String [] combatstrings = {"Elf", "Drawve", "Human", "Goblin", "Orc"};
            String [] Type = {"Infantry"};
            firstComboBox= new JComboBox (combatstrings);
            secondComboBox = new JComboBox (combatstrings);
            thirdComboBox = new JComboBox(Type);
            fourthComboBox = new JComboBox(Type);
            firstComboBox.setBounds(150,130,140,20);
            secondComboBox.setBounds(600,130,140,20);
            thirdComboBox.setBounds (150,100,140,20);
            fourthComboBox.setBounds (600,100,140,20);
     
            add(firstComboBox);
            add(secondComboBox);
            add(thirdComboBox);
            add(fourthComboBox);
     
     
            setSize (800,640);
            setTitle("Basic Title" );
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
     
            }
     
     
     
     
     
     
    public static void main(String[] args) {
            new SimpleWindow();
     
    }}

    Hi. I was here before asking how to make my values appear after pressing Go. Now I need help changing the string value into int values and input them into the algorithm. I was told to use parseInt() but when I did it wouldn't compile. Can someone help me to get this to work. The algorithm is

    if (Target’s Armor < (a Random Number between 1..100)
     
    hits
     
    else
     
    no hit

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    it wouldn't compile.
    Please post the full text of the error messages and the code that caused it.

    changing the string value into int values
    The Integer class's parseInt() will do that.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    Please post the full text of the error messages and the code that caused it.


    The Integer class's parseInt() will do that.
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method parseInt(JTextField) is undefined for the type new ActionListener(){}

    at SimpleWindow.createInterface(SimpleWindow.java:209 )
    at SimpleWindow.<init>(SimpleWindow.java:24)
    at SimpleWindow.main(SimpleWindow.java:258)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    method parseInt(JTextField) is undefined
    The error message says what the problem is. The parseInt() method does NOT take a JTextField object for a parameter. Look at the API doc for the method to see what kind of arg it takes.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    The error message says what the problem is. The parseInt() method does NOT take a JTextField object for a parameter. Look at the API doc for the method to see what kind of arg it takes.
    Sorry it seems I was supposed to use "Integer.parseInt()". But how would I go about changing something like armor, a string, to 25, an int value, using this?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    Did you read the API doc for the parseInt() method? It shows you how to use the method.
    If you have questions about the API doc for parseInt() copy the doc here and ask your questions.
    You need to understand how to read and use the API doc.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    Did you read the API doc for the parseInt() method? It shows you how to use the method.
    If you have questions about the API doc for parseInt() copy the doc here and ask your questions.
    You need to understand how to read and use the API doc.
    Where would I find the API document?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    The link: Java Platform SE 7

    Find the link to the class in the lower left section, click it and the doc is shown in the main window.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    The link: Java Platform SE 7

    Find the link to the class in the lower left section, click it and the doc is shown in the main window.
    Okay I read it and don't completely understand it. If I have a string value, like armor, Do i put it in like this?
    Integer.parseInt(Armor);
                                    System.out.printf("Armor is %d, Armor");
    It doesn't give me a int value. Please help me to understand this?

    in the API I saw this
    parseInt("Kona", 27) returns 411787
    But when I tried that with armor it didn't work.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    It doesn't give me a int value
    Where does the code save the value returned by the parseInt() method?

    Look at the definition:
    public static int parseInt(String s)
    See the "int" part? That says the method returns an int.
    When a method returns a value, the code needs to assign that value to a variable.
    For example from your code:
     int a = rand.nextInt(100) + 1;
    The nextInt() method returns a value that is assigned to the variable: a.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    Where does the code save the value returned by the parseInt() method?

    Look at the definition:
    public static int parseInt(String s)
    See the "int" part? That says the method returns an int.
    When a method returns a value, the code needs to assign that value to a variable.
    For example from your code:
     int a = rand.nextInt(100) + 1;
    The nextInt() method returns a value that is assigned to the variable: a.
    So how would I go about assigning the string value of armor to an int? Like I said
     parseInt("Kona", 27) returns 411787
    returns a value but when I try
    parseInt("Armor", 27)
    it returns nothing.

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need to input algorithems with String values.

    What int could "Armor" possibly represent?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    Take a look at how your code uses the nextInt() method that I posted in post#10.
    That method returns a value that is assigned to a variable. You need to do the same thing when you call the parseInt() method.
    var = method(args);  //  assign the value returned by method to the variable: var
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    Take a look at how your code uses the nextInt() method that I posted in post#10.
    That method returns a value that is assigned to a variable. You need to do the same thing when you call the parseInt() method.
    var = method(args);  //  assign the value returned by method to the variable: var
    Armor = parseInt("Armor", 27)

    like that?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    like that?
    What does the compiler say with that statement when you compile it?

    Copy the full text of any error messages here.

    If it compiles with no errors, What happens when you execute it? Does it work?

    What is the second arg: 27 for?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    What does the compiler say with that statement when you compile it?

    Copy the full text of any error messages here.

    If it compiles with no errors, What happens when you execute it? Does it work?

    What is the second arg: 27 for?
    I thought the second arg,27, was for radix? The API used that as an example.
    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at SimpleWindow.main(SimpleWindow.java:262)
    "

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    Can you copy the full text of the compiler's error messages?

    Your IDE's message does NOT say what the problem is. The error message needs to say what the problem is.

    Do you know what a radix is? Why are you using a radix of 27?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Norm View Post
    Can you copy the full text of the compiler's error messages?

    Your IDE's message does NOT say what the problem is. The error message needs to say what the problem is.

    Do you know what a radix is? Why are you using a radix of 27?
    That was the whole error message and No I don't know what the radix was I was basing it off the API's example.

  19. #19
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need to input algorithems with String values.

    Quote Originally Posted by Leonardo1143 View Post
    That was the whole error message and No I don't know what the radix was I was basing it off the API's example.
    You're showing the error message that is returned when you run it, but all that states is that you're trying to run uncompilable code. You need to show the error message that is returned when you try to compile the code, and you shouldn't try to run uncompilable code, but instead should fix the errors first (which is what we're trying to help you do).

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    Please copy full text of the compiler's error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    There were a lot of sample calls to parseInt(). Why did you chose 27? vs 10 or any other value if you don't know what a radix is?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    I chose 27 because it was used in the example

  22. #22
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need to input algorithems with String values.

    I think one problem is that you may be confusing Strings with variables. For instance if you have a variable named armor, it is not the same thing as the String "armor", and you can't use them interchangeably. For instance if you have

    String armor = "100";

    You can't use "armor" in a method as a substitute for the variable above.

    Re choice of 27, what example did you see it used in?

    Edit:
    Oh, I see. Yeah the Integer API does have an example of a 27 radix. This is very esoteric stuff, and something you'll likely never use. I suggestion that you don't use this example, but instead I'd use the Integer.parseInt method that takes a single String as a parameter, the one that uses the default base-10 radix.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    What about the examples that used: 10 or 2 or 16?
    You are probably used to using radix 10: 10 digits 0 to 9. 2 is for binary and 16 for hexidecimal.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to input algorithems with String values.

    Oh okay so using 10 for my example should fix it?

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to input algorithems with String values.

    Using any int value in the call to parseInt() will NOT fix any compiler errors.
    An error due to an invalid radix would happen when the program was executed, not when it was compiled.

    10 is the default if you leave the radix arg out of the call to parseInt().
    Do a Search here on the forum for parseInt for lots of sample code.

    You need to get the compiler's error messages so you can see what is wrong with the source.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: May 16th, 2012, 05:15 PM
  2. Replies: 13
    Last Post: December 6th, 2011, 02:17 PM
  3. HELP! Cant pass my input values to the paint component
    By mindmaster in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 22nd, 2011, 04:39 PM
  4. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  5. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM