Why my method don't read all of my File? ... Java? - java

I have to display all of records in my blocks (text files), and do a split to "cover" the Fields Separators, but only the first record of my blocks are diplayed. What am I doing wrong?
enter code here
public static void listAllStudents() throws IOException {
File path = new File(Descriptor.getBlockPath());
for (int i = 0; i < path.listFiles().length; i++) {
try {
FileInputStream file = new FileInputStream(Descriptor.getBlockPath() + "BLK" + i + ".txt");
InputStreamReader entrada = new InputStreamReader(file);
BufferedReader buf= new BufferedReader(entrada);
String piece = " ";
System.out.println("\nBLOCO " + i + " ------------------------------------------------------ +");
do {
if (buf.ready()) {
piece = buf.readLine();
System.out.println("\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨");
String string = " ", field[] = piece.split(Descriptor.getFieldSeparator());
string = " ";
System.out.println("CPF: " + field[0]);
System.out.println("Name: " + field[1]);
System.out.println("Course: " + field[2]);
System.out.println("Age: " + field[3]);
System.out.println("Phone: " + field[4]);
System.out.println("Active: " + field[5]);
string = " ";
}
} while (buf.ready());
buf.close();
} catch (IOException e) {
System.out.println();
}
}
}

See the documentation for the BufferedReader.readLine() method:
or null if the end of the stream has been reached
Then change your code to read the file line by line:
while ((piece = buf.readLine()) != null) {
String field[] = piece.split(Descriptor.getFieldSeparator());
if (field.length >= 6) {
System.out.println("CPF: " + field[0]);
System.out.println("Name: " + field[1]);
System.out.println("Course: " + field[2]);
System.out.println("Age: " + field[3]);
System.out.println("Phone: " + field[4]);
System.out.println("Active: " + field[5]);
}
}

Related

How to send a string to ffmpeg's input in Java

I am quite new to java, and try as I might, I can't find any examples to help me. I am running ffmpeg as a process and parsing the stderr to get various bits of data - all of which is working fine, but I want to send a "q\n" command to ffmpeg's input from a gui menu item to gracefully quit it whilst it is running when necessary. So all I want to do is send a string programmatically to ffmpeg, the equivalent of sending q return from terminal. Thanks in advance
edit here is the relevant (simplified) section of the code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Thread thread = new Thread() {
public void run()
BufferedReader error_reader,input_reader;
InputStreamReader error_isr,input_isr;
String ffmpeg_command = "ffmpeg " + overwrite + " -i " + "\"" + currentfilestring + "\"" + " " + stream + " " + test + " " + findsilence + " " + videocodec + " -b:v " + videoqual + " " + audiocodec + " -ac 2 -ab " + audioqual + " " + res + " " + aspectratio + " " + framerate + " " + "\"" + destdir + destfile + "\"";
System.out.println(ffmpeg_command);
try {
OutputStream stdout;
InputStream stdin;
InputStream stderr;
String errorstr,inputstr;
//Run the ffmpeg
Process ffmpeg = Runtime.getRuntime().exec(ffmpeg_command, null, new File(userDir));
//Get stdin,stderr + stdout
stdin = ffmpeg.getInputStream();
stderr = ffmpeg.getErrorStream();
stdout = ffmpeg.getOutputStream();
stdout.write("\r\n".getBytes());
stdout.flush();
stdout.close();
error_isr = new InputStreamReader(stderr);
error_reader = new BufferedReader(error_isr);
input_reader = new BufferedReader(input_isr);
while (!error_reader.ready()) {
}
while ((errorstr = error_reader.readLine()) != null) {
if(stopconv =="yes"){
//-------------------------------------------------------------------------------------
// TRYING TO INPUT "q\n" TO FFMPEG HERE
//-------------------------------------------------------------------------------------
}
error_isr.close();
error_reader.close();
stdin.close();
stderr.close();
jProgressBar1.setValue(0);
ffmpeg.destroy();
} catch (Exception e) {
e.printStackTrace();
}
};
thread.start();
}

Generate vCard from Contact Data by java

I would like to be able to generate a vCard given the data contained within person_contact.
Here is my code.But I am unable to generate the vcard properly.
private void m1() throws IOException {
Contact contact = buildContact();
File f = new File("contact.vcf");
FileOutputStream fop = new FileOutputStream(f);
String outputImageFilePath = "contact.vcf";
createDirectoryIfNotExists(outputImageFilePath);
if (f.exists()) {
String str = "BEGIN:VCARD\n" + "VERSION:4.0\n" + "N:" + contact.getFull_name() + ";;;\n"
+ "FN:" + contact.getFull_name() + "\n" + "ORG:" + contact.getOrganization_name()
+ "\n" + "TITLE:" + contact.getTitle() + "\n" + "TEL;TYPE="
+ contact.getPhone_2_type() + ";VALUE=uri:" + contact.getPhone_2() + "\n"
+ "TEL;TYPE=" + contact.getPhone_3_type() + ",voice;VALUE=uri:tel:"
+ contact.getPhone_3() + "\n" + "EMAIL:" + contact.getEmail() + "\n" + "FAX:"
+ contact.getFax() + "\n" + "STREET1:" + contact.getStreet1() + "\n" + "STREET2:"
+ contact.getStreet2() + "\n" + "CITY:" + contact.getCity() + "\n" + "STATE:"
+ contact.getState() + "\n"
+ "END:VCARD";
fop.write(str.getBytes());
// Now read the content of the vCard after writing data into it
BufferedReader br = null;
String sCurrentLine;
br = new BufferedReader(new FileReader("contact.vcf"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
System.out.println(f.getAbsolutePath());
}
// close the output stream and buffer reader
fop.flush();
fop.close();
System.out.println("The data has been written");
}
else
System.out.println("This file does not exist");
}
private static Contact buildContact() {
Contact contact = new Contact();
contact.setCity("xxxxxx");
contact.setCountry("xxxxxx");
contact.setEmail("test#xxxxxx.com");
contact.setFax("123456789");
contact.setFull_name("Dummy Name");
contact.setOrganization_name("Dummy-technologies");
contact.setPhone("123456789");
contact.setPhone_type("work");
contact.setPhone_2("987456321");
contact.setPhone_2_type("home");
contact.setPhone_3("564789451236");
contact.setPhone_3_type("work2");
contact.setPostal_code("500000");
contact.setState("state");
contact.setStreet1("street1");
contact.setStreet2("stree2");
contact.setTitle("company");
contact.setWebsite_url("www.text.com");
return contact;
}
It is not generating the vcard properly.Can any one help me plz.

Java: Highscore function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have created a counting game and I have tried to create a highscore method.
The problem is the program returns "null" when the "Highscore" menu is chosen. What could be causing this?
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
public class Räknesätt {
public static void main(String[]args) throws IOException {
DecimalFormat df = new DecimalFormat("0.00");
int input = Integer.parseInt(JOptionPane.showInputDialog(null, "-------------------------" + "\n\n" +
"1. Spela" + "\n" +
"2. Highscores" + "\n" +
"3. Avsluta" + "\n\n" +
"-------------------------"));
if(input == 2) {
String highscores = "";
int rader = countline("Highscores");
BufferedReader inström1 = new BufferedReader
(new FileReader("Highscores"));
for(int i = 0; i <= rader; i++) {
highscores = inström1.readLine() + "\n";
}
JOptionPane.showMessageDialog(null, highscores);
}
else if(input == 3) {
System.exit(0);
}
else if(input == 1) {
String namn = JOptionPane.showInputDialog(null, "Skriv in ditt namn");
int counter = 0;
int rätt = 0;
int fel = 0;
while(counter < 10) {
int slump1 = 1 + (int)(Math.random()*100);
int slump2 = 1 + (int)(Math.random()*100);
List<String> räknesätt = new LinkedList<String>(Arrays.asList("+",
"-",
"*",
"/"));
int räknesättRand = (int)(Math.random()*4);
String räknesättStr = räknesätt.get(räknesättRand);
counter++;
switch(räknesättStr) {
case "+":
String svarStr1 = JOptionPane.showInputDialog(null,namn + " räkna ut: " + "\n" + slump1 + " + " + slump2 + " = ");
int svar1 = Integer.parseInt(svarStr1);
if(svar1 == slump1 + slump2) {
JOptionPane.showMessageDialog(null, namn + " du räknade rätt!");
rätt++;
}
else {
JOptionPane.showMessageDialog(null, namn + " du räknade fel!");
fel++;
}
break;
case "-":
String svarStr2 = JOptionPane.showInputDialog(null,namn + " räkna ut: " + "\n" + slump1 + " - " + slump2 + " = ");
int svar2 = Integer.parseInt(svarStr2);
if(svar2 == slump1 - slump2) {
JOptionPane.showMessageDialog(null, namn + " du räknade rätt!");
rätt++;
}
else {
JOptionPane.showMessageDialog(null, namn + " du räknade fel!");
fel++;
}
break;
case "*":
String svarStr3 = JOptionPane.showInputDialog(null,namn + " räkna ut: " + "\n" + slump1 + " * " + slump2 + " = ");
int svar3 = Integer.parseInt(svarStr3);
if(svar3 == slump1 * slump2) {
JOptionPane.showMessageDialog(null, namn + " du räknade rätt!");
rätt++;
}
else {
JOptionPane.showMessageDialog(null, namn + " du räknade fel!");
fel++;
}
break;
case "/":
String svarStr = JOptionPane.showInputDialog(null,namn + " räkna ut: " + "\n" + slump1 + " / " + slump2 + " = ");
int svar = Integer.parseInt(svarStr);
if(svar == (double)slump1 / slump2) {
JOptionPane.showMessageDialog(null, namn + " du räknade rätt!");
rätt++;
}
else {
JOptionPane.showMessageDialog(null, namn + " du räknade fel!");
fel++;
}
break;
}
}
JOptionPane.showMessageDialog(null, "Resultat för: " + namn + "\n\n" +
"=========================" + "\n" +
"Antal räknade tal: " + counter + "\n" +
"Antal rätt: " + rätt + "\n" +
"Antal fel: " + fel + "\n" +
"Rättprocent: " + (double)100*rätt/(rätt+fel) + "%");
PrintWriter utström1 = new PrintWriter
(new BufferedWriter
(new FileWriter("Highscores")));
utström1.println(namn + ", " + (double)100*rätt/(rätt+fel) + "% rätt");
utström1.close();
}
}
public static int countline (String filnamn)throws IOException {
BufferedReader inström1 = new BufferedReader
(new FileReader(filnamn));
int lines = 0;
while(inström1.readLine() != null) {
++lines;
}
inström1.close();
return lines;
}
}
Code is trying to open a handle to a non-existent file by the name 'Highscores'.
Problem is being caused by the following line of code :
BufferedReader inström1 = new BufferedReader
(new FileReader("Highscores"));
The solution would be to create this file upfront, at the beginning of the program and write default contents into it.
Here is how you can go about doing it.
private static void createHighScoreFile() throws IOException {
PrintWriter printWriter = new PrintWriter
(new BufferedWriter
(new FileWriter("Highscores")));
printWriter.write("0\n");
printWriter.close();
}
public static void main(String[] args) throws IOException {
createHighScoreFile();
DecimalFormat df = new DecimalFormat("0.00");
There is also one more issue related to reading the contents of the file.
The condition in the for loop attempts to read an extra line from the file, which turns out to be null always. It needs to be fixed this way.
for (int i = 0; i <= rader; i++) {
to
for (int i = 0; i < rader; i++) {

Reading a .txt file into a parallel array

I need some assistance with my program. Using an example program that was similar I was able to get the basic layout of everything I needed to do. Right now my main questions is if my code is correct for the first part. I want it to read the data from my data file into a parallel array. I included my entire program for reference of the layout, but for now I'm just trying to read the data file into an array. Any help is greatly appreciated. Let me know if more information is needed or if my formatting is wrong.
Data File (patient.txt):
11111,Smith,Norris,Thyroid,1000.00
11112,Obama,Norris,Lasek,500.00
11113,Hoover,Norris,Dental,100.00
11114,Cena,Norris,Lasek,500.00
11115,Leto,Norris,Thyroid,1000.00
22221,Clark,Bond,Thyroid,1000.00
22222,Chang,Bond,Lasek,500.00
22223,Dent,Bond,Dental,100.00
22224,Nixon,Bond,Lasek,500.00
22225,Washington,Bond,Dental,100.00
33331,Jones,Lee,Dental,100.00
33332,Ross,Lee,Lasek,500.00
33333,Gates,Lee,Thyroid,1000.00
33334,Johnson,Lee,Thyroid,1000.00
33335,Carter,Lee,Dental,100.00
Program (class is Patient_Reports)
`
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
public class Patient_Reports {
public static void main(String[] args)
{
int selection;
String pnumber;
Patient_Reports patient = new Patient_Reports();
patient.start_system();
pnumber = patient.menu();
selection = Integer.parseInt(pnumber);
while(selection !=3)
{
if(selection==1)
patient.All_Information_Report();
else
if(selection==2)
patient.Surgeries_Doctor_Report();
else
if(selection==3)
patient.Surgeries_Type_Report();
else
if(selection==4)
patient.Doctor_Fees_Report();
else
if(selection==5)
patient.Average_Fees_Report();
pnumber = patient.menu();
selection = Integer.parseInt(pnumber);
}//while loop
patient.Exit();
System.exit(0);
}//main method
//Read Data File
int count=-1,i;
int [] id = new int [10];
String [] patient = new String [10];
String [] doctor = new String [10];
String [] surgery = new String [10];
double [] cost = new double [10];
void start_system()
{
String newLine;
try
{
//define a file valiable for Buffered read
BufferedReader Patient_Reports = new BufferedReader(new FileReader("patient.txt"));
//read lines in file until there are no more lines in the file to read
while ((newLine = Patient_Reports.readLine()) != null)
{
//there is a "," between each data item in each line
StringTokenizer delimiter = new StringTokenizer(newLine,",");
count=count+1;
id[count] = Integer.parseInt(delimiter.nextToken());
patient[count] =delimiter.nextToken();
doctor[count] =delimiter.nextToken();
surgery[count] =delimiter.nextToken();
cost[count] = Double.parseDouble(delimiter.nextToken());
}//while loop
Patient_Reports.close();
}//end try
catch (IOException error)
{
//there was an error on the file writing
System.out.println("Error on file read " + error);
}//error on read
}//end start_system
//Method for Menu of Reports
String menu()
{
String pnum;
String Output = "Reports" + "\n" +"1. All_Information_Report" + "\n" +
"2. Surgeries_Doctor_Report" + "\n" +
"3. Surgeries_Type_Report" + "\n" +
"4. Doctor_Fees_Report" + "\n" +
"5. Average_Fees_Report" + "\n" +
"6. Exit" + "\n" +
" " + "\n" +
"Select a Report >";
pnum = JOptionPane.showInputDialog(null,
Output, "",JOptionPane.QUESTION_MESSAGE);
return pnum;
}//end menu
/* Placeholder for the Reports
//All_Information_Report Report containing all of the information
void All_Information_Report()
{
System.out.println("All_Information_Report");
for (i=0; i<=count; ++i)
{
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Surgeries_Doctor_Report Report on all surgeries of a specific doctor (prompt for the doctor)
void Surgeries_Doctor_Report()
{
System.out.println("Surgeries_Doctor_Report");
for (i=0; i<=count; ++i)
{
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Surgeries_Type_Report Report on all surgeries of a specific type(Prompt for the surgery type)
void Surgeries_Type_Report()
{
System.out.println("Surgeries_Type_Report");
for (i=0; i<=count; ++i)
{
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Doctor_Fees_Report Report on the total amount of fees paid to each doctor
void Doctor_Fees_Report()
{
System.out.println("Doctor_Fees_Report");
for (i=0; i<=count; ++i)
{
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Average_Fees_Report Report on the Average Fee
void Average_Fees_Report()
{
System.out.println("Average_Fees_Report");
for (i=0; i<=count; ++i)
{
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
*/
//Store Data File in Array
void Exit()
{
try
{
BufferedWriter Patient_Reports = new BufferedWriter(new FileWriter("patient.txt"));
for (i=0; i<=count; ++i)
{
//put "," between each data item in the file
Patient_Reports.write(id[i] + "," + patient[i] + "," + doctor[i]+ "," + surgery[i] + ","+ cost[i]+ ",");
//write a new line in the file
Patient_Reports.newLine();
}//for loop
Patient_Reports.close();
}//end try
catch (IOException error)
{
//there was an error on the write to file
System.out.println("Error on file write " + error);
}//end error
}//end class
}//end exit_system`
To begin, your code is VERY confusing and violates several OOP programming conventions.
The first (and most egregious) problem is that your static class Patient_Reports contains the main method but does not utilize this static entry point to declare an object or advance. So, it would behoove you to reformat your code like this:
public class Patient_Reports {
Patient_Reports fields...
public Patient_Report() {
//start work here in Constructor
}
Patient_Reports methods...
public static void main(String[] args) {
new Patient_Reports();
}
}
To address your main question, you would want to have some sort of generalized method you could call within your class like this:
public static void writePatientReports(int[] id, String[] patient,
String[] doctor, String[] surgery, double[] cost) {
BufferedWriter Student_file = new BufferedWriter(new FileWriter("patient.txt"));
for (i=0; i<=count; ++i)
{
//put "," between each data item in the file
Student_file.write(id[i] + "," + patient[i] + "," + doctor[i]+ "," + surgery[i] + ","+ cost[i]+ ",");
//write a new line in the file
Student_file.write("\n");
}//for loop
Student_file.close();
}
The reason you were getting errors is because you had no methods attached to Patient_Reports like write() or newLine(). And even if you had, you would have needlessly declared Student_file as a BufferedWriter object.
Further, you would do greatly in furthering your knowledge of Java and general OOP by reading Java Code Conventions

How can I use a while to continuously ask for input from a user and exit the program when "quit" is typed without using system.exit()?

I am currently taking an AP Computer Science class in my school and I ran into a little trouble with one of my projects! The project requires me to create a calculator that can evaluate an expression and then solve it. I have got most of that down, but I ran into a little trouble because my teacher asked me to use a while loop to continuously ask for input and display the answer, and I am stuck on that. To end the program the user has to type in "quit" and I can't use system.exit() or any cheating thing like that, the program has to just run out of code. Does anyone have any tips?
import java.util.*;
public class Calculator {
public static void main(String[] args) {
System.out.println("Welcome to the AP Computer Science calculator!!");
System.out.println();
System.out.println("Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)");
System.out.println("or: (symbol)(space)(double)");
System.out.println();
next();
}
public static void next() {
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression, or quit to exit: ");
String expression = kb.nextLine();
next3(expression);
}
public static void next3(String expression) {
while (!expression.equals("quit")) {
next2(expression);
next();
}
}
public static void next2(String expression) {
if (OperatorFor2OperandExpressions(expression).equals("+")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) + SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("*")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) * SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("-")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) - SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("/")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) / SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("^")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + Math.pow(FirstOperandFor2OperandExpressions(expression),SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("|")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.abs(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("v")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sqrt(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("~")) {
double x = 0.0;
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + (Math.round(OperandFor1OperatorExpressions(expression))+ x));
}
else if (OperatorFor1OperandExpressions(expression).equals("s")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sin(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("c")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.cos(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("t")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.tan(OperandFor1OperatorExpressions(expression)));
}
}
public static double FirstOperandFor2OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[0];
double y = Double.parseDouble(OperandOrOperator);
return y;
}
public static double SecondOperandFor2OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[2];
double y = Double.parseDouble(OperandOrOperator);
return y;
}
public static String OperatorFor2OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[1];
return OperandOrOperator;
}
public static String OperatorFor1OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[0];
return OperandOrOperator;
}
public static double OperandFor1OperatorExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[1];
double y = Double.parseDouble(OperandOrOperator);
return y;
}
public static boolean QuitFunction(String expression) {
if (expression.equalsIgnoreCase("quit")) {
System.out.println("Goodbye!");
return false;
}
else {
return true;
}
}
}
Take a look at this code. I think this might help you in the right direction. It's similar to what you have already written except it eliminates the need for method calls in your while loop.
Scanner input = new Scanner(System.in);
while (!input.hasNext("quit")) {
String expression = input.nextLine(); // gets the next line from the Scanner
next2(expression); // process the input
}
// once the value "quit" has been entered, the while loop terminates
System.out.println("Goodbye");
Writing it this way drastically cleans up your code and prevents a new declaration of Scanner kb = new Scanner(System.in); each time an input is processed.

Categories