Java Array / Program Issue -


ok, working on java program college class , have spent many hours trying figure out doing wrong.

my program below. needs convert integer single digits , add them up. has display original number, individual digits , sum.

part of project has accept negative digits , display positive numbers , sum, array displaying -1 first number when negative number input , cannot life of me figure out how fix it.

example: input of -3456 ends displaying -1, 3, 4, 5, 6 , sum of 17 wrong.

any immensely appreciated, thanks!

import java.util.*; import javax.swing.joptionpane; //import package using dialog boxes import java.util.arrays; //import package arrays  public class project4 {    public static void main(string args[])    {     //declares , initialize variables sum & counter       int sum = 0;       int counter = 1;    //asks integer input , stores string in numinput       string numinput = joptionpane.showinputdialog(null, "enter integer: ", "user input", joptionpane.question_message);        int input = integer.parseint(numinput);//parses value of numinput integer , stores input        int numlength = string.valueof(input).length();//sets numlength length of input       int [] vararray = new int[numlength];//initilizes array match numlength        if(input == (-input))//tests negative input value       {           input = (input * (-1));//corrects negative input value              (int = 0; < numlength; i++ ) //starts loop             {                 string var = numinput.substring(i,counter);//stores value of number @ location between , counter var                 int numval = character.getnumericvalue(var.charat(0));//sets numval numeric value of character @ 0                 vararray[i] = numval;//saves numval array @ position                 sum = sum + numval;//adds sum of numbers loop goes                 counter++;//increments counter             }       }        else //starts alternate loop if input not negative value       {                      (int = 0; < numlength; i++ )              {                 string var = numinput.substring(i,counter);                 int numval = character.getnumericvalue(var.charat(0));                 vararray[i] = numval;                 sum = sum + numval;                 counter++;              }       }        joptionpane.showmessagedialog(null, "the digits of integer entered " + input + " are: " + arrays.tostring(vararray).replace("[", "").replace("]", "") + "\nthe sum is: " + sum, "numbers", joptionpane.information_message);       system.exit(0); //exits program , required when using gui     } } 

if(input == (-input)) 

doesn't test negative inputs, tests if input 0.

if(input < 0) 

tests negative input.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -