Hi there, im using a java program call Blue J and im now learning something new on java, but it quite complicated on my coding and having some problem with it or may be i have screw up with my looping and linking another new java call addIntegers with it, so i need some help from u guys,

1st, enter how many number that the user requires, the number must be positive and only accept alphabetic. If not it will prompt out "You must enter an integer" or "You must enter a positive integer"

2nd, enter the number an integer from 1 to 10, same as the 1st step must be positive and only accept alphabetic.

3rd, it will show message dialog on the number that the user have key in, for example if the number is 2, "the total of integers between 1 and 2 is 3". If it is 3, "the total of integers between 1 and 3 is 6".

Finally ask the user if they want to continue and if so repeat from the start.

below is my main coding,

import javax.swing.JOptionPane;

public class Assessment6b
{
    public static void main( String args[] )
    {
        int number;
        int[] range;
        int again = 1;
        int retry = 0;
        int index = 0;

        String numStr, rangeStr, redoStr;

        while (again==1 && retry==0)
        {
            try
            {
                numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter");
                number = Integer.parseInt(numStr);

                if (number < 1)
                {       
                    JOptionPane.showMessageDialog(null, "You must enter a positive integer");
                }

                while (retry==0 || retry==1) 
                {
                    if (number >=1)
                    {
                        number++;
                        range = new int[number];
                        for (int count = 1; count < number; count++)
                        {
                            int store=0;
                            try
                            {
                                rangeStr = JOptionPane.showInputDialog("Enter integer " + count + " in the range 1-10");
                                store = Integer.parseInt(rangeStr);

                                if (store < 1 || store > 10) 
                                {
                                    JOptionPane.showMessageDialog(null, "You must enter an integer from 1 to 10");
                                    retry = 1;
                                }

                                else
                                {
                                    range[count] = store;
                                }
                            }

                            catch (NumberFormatException e)
                            {
                                JOptionPane.showMessageDialog(null, "You must enter a integer.");
                                retry = 1;
                            }

                        }

                        for (int count = 1; count < number; count++)
                        {
                            int answer = 0;
                            answer = addIntegers.integers(range[count]);
                            JOptionPane.showMessageDialog(null, "The total of Integers between 1 and " + range[count] + " is " + answer);
                        }

                    }
                }
            }

            catch (NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "You must enter a integer.");
            }

        }    

        do
        {
            redoStr = JOptionPane.showInputDialog("Do you want to continue (y/n)?");
        }
      while (redoStr.equalsIgnoreCase("y"));

    }
} 

this is the coding to link with my main,

public class addIntegers
{
    public static int integers (int num)
    {
        int result = 0;

        for (int counter=1; counter<=num; counter++)
        {
            result = result + counter;
        }
        return result;
    }
}

hope you can understand my coding, sorry and thanks for the trouble.

Sorry, can't understand your code presented like that. If you re-post with code=java tags and proper indentation I'll try to help.

public class AddIntegers
{
	public static int integers (int num)
	{
		int result = 0;
		
		for (int counter=1; counter<=num; counter++)
		{
		[COLOR="Red"]result = result + counter;[/COLOR]
		}
		return result;
	}
}

The red line is wrong. You want to increment by 1 (one) not by actual number you just used for comparison statement which can be any of submitted numbers

PS: In the future use code tags as [code] YOUR CODE HERE [/code] or [code=java]YOUR CODE HERE [/code]

Sorry, new to this forum, i edit it to make it properly.

import javax.swing.JOptionPane;

public class Assessment6b
{
    public static void main( String args[] )
    {
        int number;
        int[] range;
        int again = 1;
        int retry = 0;
        int index = 0;
              
        String numStr, rangeStr, redoStr;
               
        while (again==1 && retry==0)
        {
            try
            {
                numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter");
                number = Integer.parseInt(numStr);
                    
                if (number < 1)
                {       
                    JOptionPane.showMessageDialog(null, ("You must enter a positive integer");
                }
                
                while (retry==0 || retry==1) 
                {
                    if (number >=1)
                    {
                        number++;
                        range = new int[number];
                        for (int count = 1; count < number; count++)
                        {
                            int store=0;
                            try
                            {
                                rangeStr = JOptionPane.showInputDialog ("Enter integer " + count + " in the range 1-10");
                                store = Integer.parseInt(rangeStr);
                                
                                if (store < 1 || store > 10) 
                                {
                                    JOptionPane.showMessageDialog (null, "You must enter an integer from 1 to 10");
                                    retry = 1;
                                }
                                
                                else
                                {
                                    range[count] = store;
                                }
                            }
                    
                            catch (NumberFormatException e)
                            {
                                JOptionPane.showMessageDialog (null, "You must enter a integer.");
                                retry = 1;
                            }
                   
                        }
                        
                        for (int count = 1; count < number; count++)
                        {
                            int answer = 0;
                            answer = addIntegers.integers (range[count]);
                            JOptionPane.showMessageDialog (null, "The total of Integers between 1 and " + range[count] + " is " + answer);
                        }
                        
                    }
                }
            }

            catch (NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "You must enter a integer.");
            }

        }    
                    
        do
        {
            redoStr = JOptionPane.showInputDialog("Do you want to continue (y/n)?");
        }
      while (redoStr.equalsIgnoreCase("y"));

    }
}

and also

public class addIntegers
{
    public static int integers (int num)
    {
        int result = 0;
        
        for (int counter=1; counter<=num; counter++)
        {
            result = result + counter;
        }
        return result;
    }
}

for example, if the user enter 3 so, "The total of integers between 1 and 3 is 6", which means 1+2+3=6.

public class AddIntegers
{
	public static int integers (int num)
	{
		int result = 0;
		
		for (int counter=1; counter<=num; counter++)
		{
		[COLOR="Red"]result = result + counter;[/COLOR]
		}
		return result;
	}
}

The red line is wrong. You want to increment by 1 (one) not by actual number you just used for comparison statement which can be any of submitted numbers

PS: In the future use code tags as [code] YOUR CODE HERE [/code] or [code=java]YOUR CODE HERE [/code]

OK, I though you looking for numbers count not sum of all of them.

So what is your problem with this code?

nvm its ok, any way is my fault because i nv explain to u properly,
im having 2 problem in this program,
1st problem is, when I key in '2' on "How many numbers do you want to enter", which means i need to have input 2 different number.
After that i was been ask to "Enter integer 1 in the range of 1-10", if i enter negative number or alphabet, which mean it cannot accept, so by right it should loop back to "Enter integer 1 in the range of 1-10" but it goes to "Enter integer 2 in the range of 1-10".

2nd problem is i cant do "Do you want to continue (y/n)?" after finish the program, i have try to put other places, but it wont prompt it or even i key in 'n' it will still continue.

OK, I though you looking for numbers count not sum of all of them.

So what is your problem with this code?

Your integers methods has a loop, but the code that calls it is also in a for (int count = 1; count < number; count++) loop. Why is that?

what do u mean? u mean why m i using the same int for the forloop?

Your integers methods has a loop, but the code that calls it is also in a for (int count = 1; count < number; count++) loop. Why is that?

By right, when i enter '2' in "How many numbers do you want to enter", which means i need to key in 2 different number, so it will prompt me to enter the 1st number "Enter integer 1 in the range of 1-10" but i choose to enter either a negative value or alphabet, so it will not accept and should loop back to "Enter integer 1 in the range of 1-10", but it continue to "integer 2" instead "of integer 1".

Another problem is i have add in "Do you want to continue (y/n)?" at the last part where the program is finish, it seems not to be working.

Im not sure where my coding went wrong, so need some assist from u guys. thank you.

Guys I have fix the problem already, thx for helping.

import javax.swing.*;

public class Assessment6
{
    public static void main( String args[] )
    {
        int number;
        int[] range;
        int retry = 0;
        
        boolean invalidNumber = true;
              
        String numStr, rangeStr, again;
        
        again = "y";
        
        while (again.equalsIgnoreCase("y"))
        {
            do
            {
                numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter");
            try
            {
                
                number = Integer.parseInt(numStr);
                    
                if (number < 1)
                {       
                    JOptionPane.showMessageDialog(null, "You must enter a positive integer");
                }
                
                else 
                {
                    number = Integer.parseInt(numStr);
                    range = new int[number];
                    
                    for (int count = 0; count < number; count++)
                    {
                        int store = 0;
                        
                        do
                        {
                            int total = count +1;
                            rangeStr = JOptionPane.showInputDialog("Enter integer " + total + " in the range 1-10");
                                
                            try
                            {
                                    store = Integer.parseInt(rangeStr);
                                
                                    if (store < 1 || store > 10) 
                                    {
                                        JOptionPane.showMessageDialog(null, "You must enter an integer from 1 to 10");
                                    }
                                
                                    else
                                    {
                                        range[count] = Integer.parseInt(rangeStr);
                                        if (range[count] >=1 || range[count]<=10)
                                        { 
                                            int answer = addIntegers.integers(range[count]);
                                            JOptionPane.showMessageDialog(null, "The total of Integers between 1 and " + range[count] + " is " + answer);
                                        }
                                    }
                                }
                    
                                catch (NumberFormatException e)
                                {
                                    JOptionPane.showMessageDialog(null, "You must enter a integer.");
                                }
                            }
                            while (range[count] < 1 || range[count] > 10); 
                        }
                            invalidNumber = false;
                            again = JOptionPane.showInputDialog("Do you want to continue (y/n)?");
                        
                        
                }
            }

            catch (NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "You must enter a integer.");
            }
        }
            while (invalidNumber);
           
        }
    }
    
    public static int addIntegers (int number )
    {
        int answer = 0;
        
        for (int count = 0; count < number; count++)
        {
            answer = answer + count;
        }
    
        return answer;
    }
}
public class addIntegers
{
    public static int integers (int num)
    {
        int result = 0;
        
        for (int i = 0; i <=num; i++)
        {
            result = result + i;
        }
        return result;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.