数据存储在名为Jobs.json的文件中,这里是文件:
[
{
"JobTitle":" Computer Programmer",
"field":"Software Development",
"Company":"Ideal Life",
"Education":"Any undergraduate degree",
"Salary":"$68,500 - $75,000 a year",
"Hours":"37.5 hours per week",
"Experience":"Solid coding chops with HTML, CSS, Javascript; this includes JQuery and Bootstrap",
"Responsibilities":"Assist in producing data exports/reports via SQL query routines",
"University":"Bachelor of Engineering:\nEnglish/Anglais (ENG4U/EAE4U preferred)\nAdvanced Functions (MHF4U)\nPhysics (SPH4U)\nChemistry (SCH4U)\nCalculus and Vectors (MCV4U)\nGrades should be 80-87% for consideration.",
"College":"N/A"
},
{
"JobTitle":"IT Specialist",
"field":"IT",
"Company":"Meriti Wealth Advisors Inc",
"Education":"a diploma/degree in Computer Science",
"Salary":"$68,500 - $75,000 a year",
"Hours":"Not given",
"Experience":"sample text",
"Responsibilities":"more sample text",
"University":"Low 90%s to consider",
"College":"N/A"
},
{
"JobTitle":"Computer Programmer",
"field":"Computer Software",
"Company":"IBM",
"Education":"Bachelor’s degree in Computer Science, Software Engineering or equivalent",
"Salary":"Not given",
"Hours":"Not given",
"Experience":"responsive web applications",
"Responsibilities":"Work alongside our multidisciplinary team of developers and designers",
"University":"Low 90%s to consider",
"College":"N/A"
},
{
"JobTitle":"Computer programmer",
"field":"Computer Software/Transportation Management",
"Company":"Degama",
"Education":"College, or other non-university certificate or diploma from a program of 1 year to 2 years",
"Salary":"$73,000.00 per year",
"Hours":"40 hours per week",
"Experience":"Experience an asset",
"Responsibilities":"Write, modify, integrate and test software code;",
"University":"N/A",
"College":"(Seneca Computer Programming Diploma)\nOntario Secondary School Diploma (OSSD) or equivalent with:\nGrade 12 English: ENG4(C) or ENG4(U)\nGrade 12 Mathematics: (C) or (U) or Grade 11 Mathematics: (U) or (M)"
},
{
"JobTitle":"Software Test Engineer",
"field":"Auto industry",
"Company":"General Motors",
"Education":"Bachelor's degree in Computer Science, Software Engineering, or equivalent field",
"Salary":"Not given",
"Hours":"Not given",
"Experience":"Advanced degrees preferred",
"Responsibilities":"Define test strategy and test plan based on software development plan",
"University":"Low 90%s to consider",
"College":"N/A"
}
]
这是我编写的java程序,我尝试在程序中保存JSON文件中的所有内容,并将作业标题放入组合框中。
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import org.json.JSONArray;
import org.json.JSONObject;
public class Main
{
static Scanner input;
static String file;
static String[] jobs;
static String[] field;
static String[] company;
static String[] education;
static String[] salary;
static String[] hours;
static String[] experience;
static String[] responsibilities;
static String[] university;
static String[] college;
static JComboBox<String> combo = new JComboBox <String>()
public static void main(String[] args)
{
try
{
input = new Scanner(new File("Jobs.json"));
}
catch(FileNotFoundException e)
{
System.out.println("error");
}
while(input.hasNextLine());
{
file+=input.nextLine();
}
final JSONArray jason=new JSONArray(file);
for(int a=0;a<jason.length();a++)
{
JSONObject object=jason.getJSONObject(a);
jobs[a]=object.getString("JobTitle");
field[a]=object.getString("field");
company[a]=object.getString("Company");
education[a]=object.getString("Education");
salary[a]=object.getString("Salary");
hours[a]=object.getString("Hours");
experience[a]=object.getString("Experience");
responsibilities[a]=object.getString("Responsibilities");
university[a]=object.getString("University");
college[a]=object.getString("College");
combo.addItem(jobs[a]);
}
JFrame jframe = new JFrame("Window");
jframe.setSize(640, 480);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
JPanel jpanel = new JPanel();
jpanel.setBackground(Color.CYAN);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
final JLabel label = new JLabel(" ");
label.setFont(new Font("Ariel", Font.PLAIN, 14));
JButton button = new JButton("Press for Info");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
label.setText(combo.getSelectedItem().toString());
}
});
//Add to the jpanel
jpanel.setLayout(layout);
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
//insets(top, left, bottom, right);
constraints.insets = new Insets(0,0,0,200);
jpanel.add(combo,constraints);
//Add Button
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 0.1;
constraints.fill = GridBagConstraints.NONE;
//insets(top, left, bottom, right);
constraints.insets = new Insets(0,0,0,0);
jpanel.add(button,constraints);
//Add label
constraints.gridx = 0;
constraints.gridy = 2;
constraints.insets = new Insets(10,0,0,0);
jpanel.add(label,constraints);
//Add the panel container to our window
jframe.add(jpanel);
jframe.setVisible(true);
}
}
答案 0 :(得分:1)
替换
while(input.hasNextLine());
带
while(input.hasNextLine())
在main method
你现在的代码会遇到无限循环。