嵌套迭代为单独的

时间:2017-06-17 06:14:20

标签: java oop nullpointerexception linked-list comparator

我有2个名单 -

[(Tandoor,Indian),(Biryani,Indian),(比萨,意大利)]

{在上面,左边的项目是右边的美食类型的食品}

[(John,Indian),(Mary,Indian),(Sam,Italian)]

{在上面,左边的人可以选择正确的菜肴类型}

我需要打印出列表的内容为 约翰坦多尔 约翰·比亚尼 玛丽坦多尔 玛丽·比亚尼 Sam Pizza

用于初始化2个给定列表,打印输出和处理Pair对象(比较,访问键和值)的API。

为什么我会在以下内容中获得nullpointerexception -

package a1;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class main {
    static class Pair implements Comparable<Pair>{
        String pairKey;
        String pairValue;

        Pair(String pairKey, String pairValue) {
            this.pairKey = pairKey;
            this.pairValue = pairValue;
        }

        public String getPairKey() {
            return pairKey;
        }

        public String getPairValue() {
            return pairValue;
        }

        public String toString() {
            return getPairKey() + " " + getPairValue();
        }

        @Override
        public int compareTo(Pair pair) {
            int compare = pairKey.compareTo(pair.getPairKey());
            if(compare == 0) {
                compare = pairValue.compareTo(pair.getPairValue());
            }
            return compare;
        }
    }
    static List<Pair> solve(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs){
        List<Pair> result= null;
        for(Pair first : teamCuisinePreferencePairs){
            for(Pair name : lunchMenuPairs)
            {
                String a1 = first.getPairValue();
                String a2 = name.getPairValue();
                String a3 = first.getPairKey();
                String a4 = name.getPairKey();
                if(first.compareTo(name)==0)
                {
                    System.out.println(a3);
                    System.out.println(a4);
                    Pair pair = new Pair(a3,a4);
                    result.add(pair);
                }
            }
        }
        return result;
    }

    private static void readAndSetParameters(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs) {
        int lunchMenuPairCount = -1;
        int teamCuisinePreferencePairCount = -1;

        String tempOption = null, tempOptionValue = null;;
        try(Scanner in = new Scanner(System.in)) {
            lunchMenuPairCount = in.nextInt();
            while(lunchMenuPairCount > 0) {
                lunchMenuPairCount--;

                tempOption = in.next();
                tempOptionValue = in.next();
                Pair pair = new Pair(tempOption, tempOptionValue);
                lunchMenuPairs.add(pair);
            }

            teamCuisinePreferencePairCount = in.nextInt();
            while(teamCuisinePreferencePairCount > 0) {
                teamCuisinePreferencePairCount--;

                tempOption = in.next();
                tempOptionValue = in.next();
                Pair pair = new Pair(tempOption, tempOptionValue);
                teamCuisinePreferencePairs.add(pair);
            }
        }
    }

    private static void print(List<?> resultPairs) {
        for (Object pair : resultPairs) {
            System.out.println(pair);
        }
    }

    public static void main(String[] args){
        List<Pair> lunchMenuPairs = new LinkedList<>();
        List<Pair> teamCuisinePreferencePairs = new LinkedList<>();
        readAndSetParameters(lunchMenuPairs, teamCuisinePreferencePairs);
        List<Pair> result = solve( lunchMenuPairs, teamCuisinePreferencePairs );
        Collections.sort(result);
        print(result);
    }
}

1 个答案:

答案 0 :(得分:1)

更改

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NavController, AlertController, ToastController, ViewController, Events, LoadingController } from 'ionic-angular';
import { WelcomePage } from '../welcome/welcome';
import { PmsServiceProvider } from '../../providers/pms-service/pms-service';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  public emailError: boolean = false;
  public passwordError: boolean = false;
  userForm: FormGroup;
  public loginStatus: boolean = true;
  public rememberChecked = true;
  Email;
  Password3;
  check;
  logined : boolean =false;
  checkStatus=1;

   toast = this.toastCtrl.create({
  message: 'Please enter correct email Id and password',
  duration: 3000,
  position: 'bottom'
})

public loading = this.loadingCtrl.create({
  content: 'Loading...',
  spinner: 'bubbles'
});

  constructor(public navCtrl: NavController, private form: FormBuilder, private alertCtrl: AlertController, private toastCtrl: ToastController, private service: PmsServiceProvider, public viewCtrl : ViewController, public events: Events,  public loadingCtrl: LoadingController) {
       console.log("in constructor");
       events.subscribe('user:created', (data)=>{
            this.Email=localStorage.getItem("email");
            this.Password3=localStorage.getItem("password");
       })
    this.userForm = form.group({
      email: [null, [Validators.required]],
      password: [null, [Validators.required]]
    })
  }    

  login(inputForm) {
       console.log(inputForm.value.email);
       console.log(inputForm.value.password);
    if (inputForm.value.email == null || inputForm.value.email == "") {
      this.emailError = true;
    } else {
      this.emailError = false;
    }

    if (inputForm.value.password == null || inputForm.value.password == "") {
      this.passwordError = true;
    } else {
      this.passwordError = false;
    }

    if (inputForm.value.email == null || inputForm.value.password == null || inputForm.value.email == "" || inputForm.value.password == "") {
         console.log("one is null");
      this.loginStatus = false;
    }
    else {
      this.loginStatus = true;
    }

    //CODE FOR TESTING THE EMAIL ID AND PASSWORD IS CORRECT 
    // Hit to the Api.
    if (this.loginStatus == true) {
           this.loading.present().then(()=>{
                this.service.checkUser(inputForm.value).subscribe(resData => {
                    //  console.log(resData.data.body);
                    console.log(resData);
                    if(this.loading){
                         this.loading.dismiss();
                         // this.loading =null;
                    }

                    //  console.log(resData.success);
                    //  console.log(resData.body[0]);
                  if (resData.success == true) {
                       if(this.rememberChecked ==true){
                            localStorage.setItem("email",inputForm.value.email);
                            localStorage.setItem("password",inputForm.value.password);
                            localStorage.setItem("user_id",resData.data.body[0].id);
                       }else{
                            localStorage.setItem("email","");
                            localStorage.setItem("password","");
                       }
                    // console.log(resData.respData[0].id);
                    localStorage.setItem("token",resData.data.mytoken);
                    console.log(resData.data.body[0].first_name);

                    this.navCtrl.push(WelcomePage, { id: resData.data.body[0].id, name : resData.data.body[0].first_name}).then(()=>{
                         const index = this.navCtrl.getActive().index;
                         this.navCtrl.remove(0,index);
                    });
                    // this.navCtrl.setRoot(WelcomePage);
                  }
                  else {
                    this.toast.present();;
                  }
                })
           });
     ;
    }
  }

}

static List<Pair> solve(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs){
    List<Pair> result= null;
    ...