Posts

Showing posts with the label java.util.scanner

How to repeat input using a while loop with a sentinal

How to repeat input using a while loop with a sentinal I need help with regards to where to put a while loop on this code. I just began studying Java a few weeks ago. I would like to write in a sentinel value of -1, if entered, the program shall exit. As long as user input is not -1, keep asking repeating the program. When I put the while loop, "while(currentPop != -1)" , below the first question, "Enter current population", the program runs its first course successfully. However, it does not loop back to the first question. Instead, it goes directly to the second question, "Enter birth rate:". How shall I proceed and make sure that the first question keeps getting asked after running through the loops? Thank you all! import java.util.Scanner; public class Population { public static void main(String args) { Scanner scan = new Scanner(System.in); double birthRate, deathRate; double currentPop = 0; double newPop; ...

Iterating though a text file in Java so that each line has information about that line

Iterating though a text file in Java so that each line has information about that line Ok, so I really didn't know how to say it right for the title so this should shed some light on the situation. I'm making a palindrome program in Java. In every which way you look at it, it works just fine. It reads in a file using Scanner , searches through the entire file and outputs if that line in the text file is a palindrome. If you have any special characters or caps it deletes them and turns everything to lowercase. Scanner My issue is that after the check is done on each line, I want to show some extra information next to the result. Each line should show how many words are in the line, how many characters and if its a palindrome or not. Anyway here is the code, hopefully someone can help me figure this out. Thanks. import java.io.File; import java.util.Scanner; public class Palindrome { public static void main(String args) { //Global Variables Scanner cScan = nul...