How PostgreSQL database communicate with Rest-full web service with SPRING BOOT application ??

RESTFUL WEB SERVICE CRUD OPERATIONS WITH SPRING BOOT

….this is a Spring Boot crud application using ‘PostgreSQL’ database
but i don’t know Where is the actual implementation of “DefaultEmployeeService” Class. i need to understand how the request knows that all data to retrieve it and POST to what ?? and delete from what?? and so on…
Where is data base Queries ??!

Sorry, it’s the first time for me to use Spring-boot and PostgreSQL and all websites put the same example and i can’t understand how java code interact and connect with DB ??.

Switch on an if statement?

I am a bit of a beginner with coding, so can someone please tell me if it is possible to do something like this in Java, in a void result type?

switch (if this != null {return this;}
            else {return that;}) {

    case "This": blah
        break;
    case"That": blah
        break;
}

Also, can anyone tell me the most efficient way to do a switch statement with two variables? Thanks! 🙂

Clear text inside the JTextField in java

I am working on my BMI calculator project in which I have this JButton called “Clear” which will clear all the text from the JTextField. I tried feetText.setText(""); but it didn’t work. What can I possibly do?

import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class BMI extends JFrame implements ActionListener {   
       public static void main(String[] args) {
          BMI runner = new BMI();
          runner.setSize(450, 600);
          runner.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          runner.setVisible(true);
       }

       JTextField feetText, inchesText, weightText, waistText, neckText, hipsText;
       JLabel feet, height,weightLabel, pounds, waist, neck, gender, maleLabel, femaleLabel;
       JLabel inches, inches2, inches3, inches4, info1, info2, info3, info4;
       JLabel activityLevel, sedentaryLabel, moderateLabel, activeLabel;
       JLabel bmi, ratio, pbf, hips, lbm; 
       JRadioButton male, female, sedentary, moderate, active;
       JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
       JPanel panel9, panel10, panel11, panel12, panel13, panel14, panel15;
       JButton clear, calculate;
       Font font, font2, font3, font4;
       double getBMI;
       double roundedWTHR;
       JLabel one, two;

       public BMI() {
          JFrame f = new JFrame("BMI and Body Fat Calculator");
             f.setSize(450, 600);
             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

             //Initialize the panels and set their layout

             panel1 = new JPanel();
             panel1.setLayout(new FlowLayout());
             panel2 = new JPanel();
             panel2.setLayout(new FlowLayout());
             panel3 = new JPanel();
             panel3.setLayout(new FlowLayout());
             panel4 = new JPanel();
             panel4.setLayout(new FlowLayout());
             panel5 = new JPanel();
             panel5.setLayout(new FlowLayout());
             panel6 = new JPanel();
             panel6.setLayout(new FlowLayout());
             panel7 = new JPanel();
             panel7.setLayout(new FlowLayout());
             panel8 = new JPanel();
             panel8.setLayout(new FlowLayout());
             panel9 = new JPanel();
             panel9.setLayout(new FlowLayout());
             panel10 = new JPanel();
             panel10.setLayout(new FlowLayout());
             panel11 = new JPanel();
             panel11.setLayout(new FlowLayout());
             panel12 = new JPanel();
             panel12.setLayout(new FlowLayout());
             panel13 = new JPanel();
             panel13.setLayout(new FlowLayout());
             panel14 = new JPanel();
             panel14.setLayout(new FlowLayout());
             panel15 = new JPanel();
             panel15.setLayout(new FlowLayout());

             //Height
             height = new JLabel("Height: ");
             feetText = new JTextField(10);
             feet = new JLabel(" Feet");
             inchesText = new JTextField(10);
             inches = new JLabel("inches");

             feetText.setActionCommand("height");
             feetText.addActionListener(this);

             //Weight
             weightLabel = new JLabel("Weight: ");
             weightText = new JTextField(10);
             pounds = new JLabel("  pounds");

             //Waist
             waist = new JLabel("Waist: ");
             waistText = new JTextField(10);
             inches2 = new JLabel("inches");

             //Neck
             neck = new JLabel("Neck: ");
             neckText = new JTextField(10);
             inches3 = new JLabel("inches");

             //Hips
             JLabel hips = new JLabel("Hips (Female): ");
             hipsText = new JTextField(10);       
             inches4 = new JLabel("inches");

             //Gender
             gender = new JLabel("Sex: ");
             male = new JRadioButton();
             maleLabel = new JLabel("Male");
             female = new JRadioButton();
             femaleLabel = new JLabel("Female");

             //Level of activity
             activityLevel = new JLabel("Level of Activity: ");
             sedentary = new JRadioButton();
             sedentaryLabel = new JLabel("Sedentary");
             moderate = new JRadioButton();
             moderateLabel = new JLabel("Moderate");
             active = new JRadioButton();
             activeLabel = new JLabel("Active");

             //Clear and Calculate
             clear = new JButton("Clear");
             calculate = new JButton("Calculate");

             calculate.addActionListener(this);
             clear.setActionCommand("clear");

             //Body Mass Index
             bmi = new JLabel("Body Mass Index: ");
             font = new Font("Arial Black", Font.BOLD,12);
             bmi.setFont(font);

             //Waist-To-Height Ratio
             ratio = new JLabel("Waist-To-Height Ratio: ");
             font2 = new Font("Arial Black", Font.BOLD,12);
             ratio.setFont(font2);

             //Percentage Body Fat
             pbf = new JLabel("Percentage Body Fat: ");
             font3 = new Font("Arial Black", Font.BOLD,12);
             pbf.setFont(font3);

             //Lean Body Mass
             lbm = new JLabel("Lean Body Mass: ");
             font4 = new Font("Arial Black", Font.BOLD,12);
             lbm.setFont(font4);

             //Information 1
             info1 = new JLabel("");

             //Information 2
             info2 = new JLabel("");

             //Information 3
             info3 = new JLabel("");

             //Information 4
             info4 = new JLabel("");

             //Adding components to panel1
             panel1.add(height);
             panel1.add(feetText);
             panel1.add(feet);
             panel1.add(inchesText);
             panel1.add(inches);

             //Adding components to panel2
             panel2.add(weightLabel);
             panel2.add(weightText);
             panel2.add(pounds);

             //Adding components to panel3
             panel3.add(waist);
             panel3.add(waistText);
             panel3.add(inches2);

             //Adding components to panel4
             panel4.add(neck);
             panel4.add(neckText);
             panel4.add(inches3);

             //Adding components to panel5
             panel5.add(hips);
             panel5.add(hipsText);
             panel5.add(inches4);

             //Adding components to panel6
             panel6.add(gender);
             panel6.add(male);
             panel6.add(maleLabel);
             panel6.add(female);
             panel6.add(femaleLabel);

             /*The following code prohibits selecting 
             both the JRadioButtons selected at the same time*/

             ButtonGroup genderGroup = new ButtonGroup();
             genderGroup.add(male);
             genderGroup.add(female);

             //Adding components to panel7
             panel7.add(activityLevel);
             panel7.add(sedentary);
             panel7.add(sedentaryLabel);
             panel7.add(moderate);
             panel7.add(moderateLabel);
             panel7.add(active);
             panel7.add(activeLabel);

             /*The following code prohibits selecting 
             both the JRadioButtons selected at the same time*/

             ButtonGroup activityGroup = new ButtonGroup();
             activityGroup.add(sedentary);
             activityGroup.add(moderate);
             activityGroup.add(active);

             //Adding components to panel8
             panel8.add(clear);
             panel8.add(calculate);

             //Adding a component to panel9
             panel9.add(bmi);

             //Adding a component to panel10
             panel10.add(ratio);

             //Adding a component to panel11
             panel11.add(pbf);

             //Adding a component to panel12
             panel12.add(lbm);

             //Adding components to panel13
             panel13.add(info1);
             panel13.add(info2);

             //Adding components to panel14
             panel14.add(info3);

             //Adding components to panel15
             panel15.add(info4);

             //Set the layout
             BoxLayout boxLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
             setLayout(boxLayout);

             //Adding panels to the frame
             add(panel1);
             add(panel2);
             add(panel3);
             add(panel4);
             add(panel5);
             add(panel6);
             add(panel7);
             add(panel8);
             add(panel9);
             add(panel10);
             add(panel11);
             add(panel12);
             add(panel13);
             add(panel14);
             add(panel15);

             setVisible(true);
       }

       public void actionPerformed(ActionEvent event) {
          double feet = Double.parseDouble(feetText.getText());
          double inches = Double.parseDouble(inchesText.getText());
          double weight = Double.parseDouble(weightText.getText());
          double waist = Double.parseDouble(waistText.getText());
          double neck = Double.parseDouble(neckText.getText());
          double hip = Double.parseDouble(hipsText.getText());

          double heightInInches = (feet * 12) + inches;
          double heightInCm = (feet * 30.48) + (inches * 2.54);

          if(event.getActionCommand().equals("clear")) {
             feetText.setText("");
          }

          //Calculating the Percentage Body Fat
          if(male.isSelected()) {
             double percentageBodyFat = 495/1.0324 - 0.19077 * (Math.log10(waist*2.54 - neck*2.54)) + 0.15456 * (Math.log10(feet*12*2.54 + inches*2.54)) - 450;
             double pbfRounded = Math.round(percentageBodyFat * 10.0) / 10.0;
             pbf.setText("Percent Body Fat: " + pbfRounded);
          }

          if(female.isSelected()) {
             double percentageBodyFat = (495/(1.29579-0.35004*Math.log10(waist*2.54+hip*2.54-neck*2.54) + 0.22100 * Math.log10(feet*12*2.54 + inches*2.54))-450);
             double pbfRounded = Math.round(percentageBodyFat * 10.0) / 10.0;
             double bodyFatPercent = pbfRounded;
             pbf.setText("Percent Body Fat: " + pbfRounded + "%");
          }

          //Calculating the Body Mass Index
          double bodyMassIndex = Math.round(703 * (weight / (heightInInches*heightInInches)*10.0)) / 10.0;
          double getBMI = bodyMassIndex;
          bmi.setText("Body Mass Index: " + bodyMassIndex + " kg/m²");

          //Calculating the Waist-To-Height Ratio
          double wthr = waist/heightInInches;
          double roundedWTHR = Math.round(wthr * 100.0) / 100.0;
          ratio.setText("Waist-To-Height Ratio: " + roundedWTHR);

          //Calculating the Lean Body Mass
          if(male.isSelected()) {
             double leanBodyMass = (0.32810 * weight) + (0.33929 * heightInCm) - 29.5336;
             double lbmRounded = Math.round(leanBodyMass * 10.0) / 10.0;
             lbm.setText("Lean Body Mass: " + lbmRounded + "lb");
          }

          if(female.isSelected()) {
             double leanBodyMass = (0.29569 * weight) + (0.41813 * heightInCm) - 43.2933;
             double lbmRounded = Math.round(leanBodyMass * 10.0) / 10.0;
             lbm.setText("Lean Body Mass: " + lbmRounded + "lb");
          }

          if(getBMI < 15) {
             info1.setText("Your Bmi is severly low");
             info3.setText("You are severly underweight probably due to usual mass distributiuon");
          } else if(getBMI <= 16) {
             info1.setText("Your Bmi is extremely low");
             info3.setText("You are extremely underweight probably due to usual mass distribution");
          } else if(getBMI <= 18.5) {
             info1.setText("Your BMI is lower than usual");
             info3.setText("You may be underweight probably due to usual mass distribution");
          } else if(getBMI <= 25 ) {
             info1.setText("Your BMI is normal");
             info3.setText("Your weight and mass distribution is normal");
          } else if(getBMI <= 30) {
             info1.setText("Your BMI is greater than normal");
             info3.setText("You may be overweight or have unusual mass distribution");
          } else if(getBMI <=35) {
             info1.setText("Your BMI is very much greater than normal");
             info3.setText("You are overweight or have unusual mass distribution");
          } else if(getBMI <= 39.9) {
             info1.setText("Your BMI is severly greater than normal");
             info3.setText("You are extremely overweight or have unusual mass distribution");
          } else if(getBMI >= 40) {
             info1.setText("Your BMI is extremely greater than normal");
             info3.setText("You are severly overweight or have unusual mass distribution");
          }

          if(male.isSelected() && roundedWTHR < 0.35) {
             info2.setText("and your weight-to-height ratio is severly low");
          }
          else if(male.isSelected() && roundedWTHR <= 0.43) {
             info2.setText("and your weight-to-height ratio is extremely low");
          }
          else if(male.isSelected() && roundedWTHR <= 0.46) {
             info2.setText("and your weight-to-height ratio is lower than normal");
          }
          else if(male.isSelected() && roundedWTHR <= 0.53) {
             info2.setText("but your weight-to-height ratio is normal");
          }
          else if(male.isSelected() && roundedWTHR <= 0.58) {
             info2.setText("and your weight-to-height ratio is a little higher than usual");
          }
          else if(male.isSelected() && roundedWTHR <= 0.63) {
             info2.setText("and your weight-to-height ratio is extremely higher than normal");
          }
          else if(male.isSelected() && roundedWTHR > 0.63) {
             info2.setText("and your weight-to-height ratio is severly higher than normal");
          }

          if(female.isSelected() && roundedWTHR < 0.35) {
             info2.setText("and your weight-to-height ratio is severly low");
          }
          else if(female.isSelected() && roundedWTHR <= 0.42) {
             info2.setText("and your weight-to-height ratio is extremely low");
          }
          else if(female.isSelected() && roundedWTHR <= 0.46) {
             info2.setText("and your weight-to-height ratio is lower than normal");
          }
          else if(female.isSelected() && roundedWTHR <= 0.49) {
             info2.setText("but your weight-to-height ratio is normal");
          }
          else if(female.isSelected() && roundedWTHR <= 0.54) {
             info2.setText("and your weight-to-height ratio is a little higher than usual");
          }
          else if(female.isSelected() && roundedWTHR <= 0.58) {
             info2.setText("and your weight-to-height ratio is extremely higher than normal");
          }
          else if(female.isSelected() && roundedWTHR > 0.58) {
             info2.setText("and your weight-to-height ratio is severly higher than normal");
          }

          if(sedentary.isSelected()) {
             info4.setText("Your diet should contain at least " + Math.round(0.36*weight) + " grams of protein per day.");
          }

          if(moderate.isSelected()) {
             info4.setText("Your diet should contain at least " + Math.round(0.4*weight) + " grams of protein per day.");
          }

          if(active.isSelected()) {
             info4.setText("Your diet should contain at least " + Math.round(0.5*weight) + " grams of protein per day.");
          }
       }
    }

switch fragment code to activity code

hi i am bigineer on android ,i have an app that contain a histroy fragment I am trying to convert my Fragment Code to Activity code !(i want to start this activity after button click) but i don’t knew how to that so please help me

here my fragment :

public class HistoryFragment extends Fragment implements MainActivity.FragmentRefresh {

    ImageView ivSettings;
    private FragmentActivity mContext;
    private RecyclerView rvInsta;

    //DB
    private DBController dbcon;
    private ImageRecyclerAdaptor imageRecyclerAdaptor;

    public static HistoryFragment newInstance() {
        //Bundle args = new Bundle();
        //args.putString(ARG_PAGE, title);
        HistoryFragment fragment = new HistoryFragment();
        //fragment.setRetainInstance(true);
        //fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("Tag1","MoviesFrag");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_history, container, false);
        mContext =getActivity();
        //DB
        dbcon = new DBController(mContext);

        rvInsta= (RecyclerView) rootView.findViewById(R.id.rvInstaImages);
        imageRecyclerAdaptor = new ImageRecyclerAdaptor(mContext);
        rvInsta.setAdapter(imageRecyclerAdaptor);
        rvInsta.setLayoutManager(new LinearLayoutManager(mContext));

        rvInsta.setHasFixedSize(true);
        rvInsta.setItemViewCacheSize(20);
        rvInsta.setDrawingCacheEnabled(true);
        rvInsta.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        //rvInsta.s/

        imageRecyclerAdaptor.notifyDataSetChanged();
        return rootView;
    }

    @Override public void refresh() {
        if (imageRecyclerAdaptor!=null) {
            imageRecyclerAdaptor.onRefreshh();
        }
    }
}

org.json.JSONException: No value for data

I try to make a android application but I get title’s exception. My first goal is to display in TextView one character from json. After that I would like to go to the next character after click the button.The code is for a school project. I try for hours to find what goes wrong but I can not find the error.I have read a lot of stackoverflow questions and their answers but the answers are sophisticated for every piece of code.

package com.example.pirama3.pirama3;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {
     public List<String> list =new ArrayList<String>();
    public String[] ls= new String[3998];




    public void loadGrades(View view){
        Resources res = getResources();
        InputStream is = res.openRawResource(R.raw.test02);
        Scanner scanner = new Scanner(is);
        StringBuilder builder = new StringBuilder();
        while(scanner.hasNextLine()){
            builder.append(scanner.nextLine());
        }

        parseJson(builder.toString());
    }

    private void parseJson(String s) {
        TextView txt = (TextView) findViewById(R.id.abc);

        StringBuilder builder = new StringBuilder();

        try{
            JSONObject root = new JSONObject(s);
            JSONArray dt = root.getJSONArray("data");
            for(int i=0;i<3999;i++){
                JSONObject chara = dt.getJSONObject(i);
                Log.d("aaa",chara.getString("character"));
                ls[i]=chara.optString("character");
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        //Log.d("abcc",list.get(0));
        //txt.setText(s);
        txt.setText(ls[1]);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView txt = (TextView) findViewById(R.id.abc);

        //Log.d("abcc",list.get(0));
        //System.out.println(list.get(0));Resources res = getResources();
        Resources res = getResources();
        InputStream is = res.openRawResource(R.raw.test02);
        Scanner scanner = new Scanner(is);
        StringBuilder builder = new StringBuilder();
        while(scanner.hasNextLine()){
            builder.append(scanner.nextLine());
        }
        try {
            JSONObject root = new JSONObject(builder.toString());
            JSONArray dt = root.getJSONArray("data");
            //ls = new String[dt.length()];
        }catch (JSONException ab){
            ab.printStackTrace();
        }
        txt.setText(ls[1]);

    }

}

My json file is like below

{
data:[
{
 character:"a",
pronunciation:"a"
},
{
character:"b",
"pronunciation:"b"
}
]
}

This is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.pirama3.pirama3.MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="loadGrades"
        android:text="@string/button_load_grades"/>
    <TextView
        android:id="@+id/abc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:text="@string/text_student_info" />

</LinearLayout>

Encapsulation Java Coding Error [on hold]

I am working on the code but facing an error. My problem is that I have initialized this.tonner_Lvl to 35 but “System.out.println(this.tonner_Lvl )” prints out 0.

public class Printer {
    private int tonner_Lvl;
    private int pgs;
    private boolean is_duplex;
    Scanner sc = new Scanner(System.in);

public Printer(int tonner_Lvl, int pgs, boolean is_duplex) {

    if(this.tonner_Lvl <= 0 ) {
        this.tonner_Lvl = 0;

    }
    else if (this.tonner_Lvl >= 100)
    {
        this.tonner_Lvl =100;
    }
    else 
        this.tonner_Lvl = tonner_Lvl;
    System.out.println(this.tonner_Lvl ); // Why is it printing 0 while I 
                                           // have initialised it
    this.pgs = pgs;
    System.out.println(this.pgs);
    this.is_duplex = is_duplex;
    System.out.println(this.is_duplex);
}


public class Main {
    public static void main(String[] args) {
        Printer printer = new Printer(35, 12, true);
       //printer.print();
        printer.tonner();

     }
 }