Results 1 to 6 of 6
  1. #1
    @eddie.com is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Exclamation output from an array

    Hi all and thanks fo looking at this for me!!!!

    I have a project that gets the user input to initialize an array size, and then another user input to ask the values for the subscript. this has to be done using JOptionPane. For some reason when I try to display the output it will not tell me the inputed value for the array subscripts. this is a small piece of code. all the books and the forums I read look as my code looks apart from the input is collected from the scanner class. where am I going wrong

    thank you..

    import javax.swing.*;
    public class SquareCubeArray
    {
    public static void main ()
    {
    String inptNumber;
    String inptPlays;
    int [] value;
    int index =0;

    // this should collect the Array size

    inptPlays=JOptionPane.showInputDialog("how many numbers do you want to enter");
    int plays = Integer.parseInt(inptPlays);
    value = new int[plays];

    for(int count=0;count<plays;count++)
    {
    //this part should!! collect the numbers for the array

    inptNumber=JOptionPane.showInputDialog("Enter a Number between 1 and 100");
    value[index] = Integer.parseInt(inptNumber);


    }
    for(int count=0;count<plays;index++)
    {
    System.out.println(value[index]);

    }
    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    value[index] = Integer.parseInt(inptNumber);
    is index here the actual "index" of your for loop? If not, what variable should be in there instead?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Basically what Fubarable trying to say is, all the time you are hanging with the same index. It's not iterate(change) at all.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Are looking something like this to do?

    Java Code:
    import javax.swing.*;
    
    public class SquareCubeArray {
        
        public static void main (String[] args) {
            String inptNumber;
            String inptPlays;
            int [] value;
            //int index =0; // No need this anymore 
    
            // this should collect the Array size
            inptPlays = JOptionPane.showInputDialog("how many numbers do you want to enter");
            int plays = Integer.parseInt(inptPlays);
            value = new int[plays];
    
            for(int count=0;count<value.length;count++) {
                //this part should!! collect the numbers for the array
                inptNumber = JOptionPane.showInputDialog("Enter a Number between 1 and 100");
                value[count] = Integer.parseInt(inptNumber);
            }
            
            for(int count=0;count<value.length;count++) {
                System.out.println(value[count]);
            }
        }
    
    }

  5. #5
    @eddie.com is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Red face

    Thank you all for the quick reply to my query

    The answer that has been given has only highlighted the fact that I need to go back and start again reading from my text book as not understanding the basic construct for a for loop is pretty bad. My lecturer states that we need to have a well laid out plan (Pseudocode and test plan before we even consider writing code). Although I seem to go the wrong way about it.

    if(understanding==notMuch)
    JOptionPane.showMessageDialog("get your finger out and read the text book")

    else
    JOptionPane.showMessageDialog("Go forth and contribute to the forums");

    Thanks again

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes it is, you should better to read a text book about the Java basis. And all the time, before starting work what you have to do is, plan the project flow well. It doesn't matter you following the way of pseudo code, UML or else. Best thing is spend lots of time on that. Coding doesn't take no longer actually, but designing/planing takes long time of period.

Similar Threads

  1. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 02:42 PM
  2. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 11:14 PM
  3. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 05:05 AM
  4. JSP to output Java String Array
    By irenavassilia in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-31-2008, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •