Вопрос

I'm getting an error when I try to compile this application. It seeks to add arbitrarily long numbers by inputting numbers into an array of LinkedLists type .

Here is my code, the error is on "if(Lists.theLists[index].get(0) != null)"

import java.util.*;
/**
 *
 * @author --
 */
public class longNumbers 
{ 
 private List<Integer> [] theLists; 

 public longNumbers() //default constructor
 { 
    this.theLists = new LinkedList[11]; 
    for (int i=0; i<11; i++) 
    this.theLists[i]= new LinkedList<>(); 
 } 

public void add(int location, int digit) 
 { 
     theLists[location].add(digit);
 //add digit at head of LinkedList given by location 
 } 

public int remove(int location) 
 {  
    return theLists[location].remove(location);
  //remove a digit from LinkedList given by location 
 } 

public boolean isEmpty(int location) 
 { 
    return theLists[location].isEmpty();
 //check for an empty LinkedList given by location 
 } 

 public static void main(String[] args) 
 {
 //Intialize Scanner Object
 Scanner stdIn = new Scanner(System.in); 

 //Local Variables
 int digit;
 int carry = 0;
 int numberAt = 0;
 int largestNumLength = 0;
 char[] digits;
 String number;
 boolean userWantstoQuit = false;
 longNumbers Lists = new longNumbers();



 //Explain purpose of program and prompt for user input
 System.out.println("The program will enter up to 10 numbers and add them up.");
 System.out.println();

 while(!userWantstoQuit && numberAt != 9)
 {
    System.out.print("Enter a number, enter -1 to quit entry phase: ");
    number = stdIn.nextLine();

    if((number.compareTo("-1")) == 0)
        userWantstoQuit = true;
    else{
        digits = new char[number.length()];
        for(int i=0;i<number.length();i++)
            digits[i] = number.charAt(i);
        for(int i=0;i<number.length();i++){
           int tempValue = digits[i] - 48;
           try
           {
             Lists.add(numberAt, tempValue);
           }
              catch(NumberFormatException nfe)
              {
               System.out.println("Invalid Input. Please try again.");
               break;
              }
           if(i == (number.length() - 1))
               numberAt++;
           if(number.length() > largestNumLength)
               largestNumLength = number.length();
         }
    }
 }


for(int j=0;j<largestNumLength;j++)
{
 int tempDigit = 0;
 int index = 0;

 while(index < numberAt+1)
 {
 if(Lists.theLists[index].get(0) != null){//here is where the error occurs
    tempDigit += Lists.theLists[index].get(0);
    Lists.remove(0);                          
    }
 index++;
 }

 digit = carry + tempDigit;
 if(j < numberAt){
 carry = digit/10;
 digit = digit%10;
 }
 Lists.add(10, digit);
}

System.out.print("The sum of the numbers is: ");

for(int i=0;i<Lists.theLists[10].size();i++)
 {
    System.out.print(Lists.theLists[10].get(i));
 }

System.out.println();
System.out.println();
System.out.println();

 }//end main 

}//end class    
Это было полезно?

Решение

The numberAt variable stores the total numbers seen. So

while(index < numberAt+1)

Should be

while(index<numberAt)

Moreover, the while loop condition for accepting up to 10 numbers should be

while(!userWantsToQuit && numberAt!=10)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top