Write a program that asks the user to enter the time as a sequence of digits and then displays the time as "minutes:seconds". Assume that if two or fewer digits are entered that the digits represent seconds. In this case, display a zero before the colon. If three or more digits are entered, the last two represent seconds.
Output needs to be:
Enter cook time-> 123
Your time-> 1:23
According to the instructions how does one create this sequence? I know how to scan in the user's time. I just need help differentiating between minutes and seconds.
There's probably no end of ways you might achieve this, you could use modular maths, convert the input to a String and convert that to a char array or you could just use String#substring
For example...
int value = 1;
String text = Integer.toString(value);
if (text.length() < 2) {
if (text.length() == 1) {
text = "0:0" + text;
} else {
text = "0:" + text;
}
} else {
text = text.substring(0, text.length() - 2) + ":" + text.substring(text.length() - 2);
}
And yes, you could just have a 3 way if-else instead of the compound if, but that's up to you
Take a closer look at String#substring(int) and String#substring(int, int) for more details
You should also take a look at the Strings trail
Related
Trying to design a simple lottery program. Everything works except checking if the numbers entered are between 1 to 59.
Exercise says the numbers must be stored in a String variable.
so
if(num<0 || num>59) //wont work for me
Tried making another variable
int numConverted = Integer.parseInt(num)
We haven't covered converting String to int in class though so I don't think this is what expected. Got confused trying that way anyway so probably this is wrong.
Here is the code I have currently.
{
Scanner scan = new Scanner(System.in);
String num=""; //num variable is empty untill user inputs numbers
for(int i =0; i<6; i++)
{
System.out.println("Enter your number between 1-59");
num = num +" "+ scan.nextLine();
}
System.out.println("Ticket printed £2. Your numbers are " + num);
}
In your posted code it's obvious that you want the User to supply 6 specific numerical values. These values are appended to the String variable named num (space delimited). You need to obviously do a few things here:
1) Make sure the value supplied by the user is indeed a numerical value;
2) Make sure the numerical values supplied fall within the minimum and maximum scope of the lottery itself (which you have stated is: 1 to 59);
3) Make sure the number entered by the User hasn't been supplied already.
You've been tasked to store the entered values into a String data type variable and that is all fine but at some point you want to carry out value comparisons to make sure that all the entered values actually play within the limits of the lottery.
When the User completes his/her entries, you end up with a space delimited string held in the num string variable. You now need to make sure that these values entered are indeed....numbers from 1 to 59 and none contain alpha characters.
In my opinion (and this is only because you need to store entered values into a String variable), it's best to use your String variable to gather User input, then test the input to make sure it is indeed a string representation of an actual integer number. Once this is established then we test to make sure if falls within the value min/max limits (1-59). Now we need to test to make sure the number entered hasn't already been entered before for this ticket.
Of course with each test described above, if one fails then the User should be prompted to re-enter a proper value. You can do this by utilizing a while loop. Plenty examples of this in StackOverflow but here's a quick example:
Scanner scan = new Scanner(System.in);
String ticketNumbers = "";
for(int i = 0; i < 6; i++) {
Boolean isOK = false;
while (!isOK) {
System.out.println("\nPlease enter your desired 6 ticket numbers:\n"
+ "(from 1 to 59 only)");
String num = scan.nextLine();
//Is the string entered an actual integer number?
//We use the String.matches() method for this with
//a regular expression.
if(!num.matches("\\d+")) {
System.out.println("You must supply a numerical value! "
+ "Try Again...");
continue;
}
if (ticketNumbers.contains(num + " ")) {
System.out.println("The number you supplied has already been chosen!"
+ " Try Again...");
continue;
}
if (Integer.parseInt(num) >= 1 && Integer.parseInt(num) <= 59) {
ticketNumbers+= num + " ";
isOK = true;
}
else {
System.out.println("The number you supply must be from "
+ "1 to 59! Try Again...");
}
}
}
System.out.println("Ticket printed £2. Your numbers are " + ticketNumbers);
How about -
if(Integer.parseInt(num) < 0 || Integer.parseInt(num) > 59)
This should work, place it after the input.
If it works, please mark this as correct, I need the rep!!!
Easy way would be add available numbers (suppose it wont grow more than 60. You can use a loop to add to this as well)
String numbers[] = {"1","2","3", "..."};
Then inside the loop
Arrays.asList(numbers).contains(num);
You can remove prefixing zero in order avoid conflicts with values like '02'
Here everything is String related.
If you don't want to explicitly convert to int, you could use a regular expression.
if (num.matches("[1-5]?[0-9]")) {
...
This checks whether the String consists of (1) maybe a digit from 1 to 5, followed by (2) definitely a digit from 0 to 9. That'll match any number in the range 0-59.
If you've got a whole series of numbers separated by spaces, you could expand this to cover a whole series like this.
if (num.matches("([1-5]?[0-9]\\s+)*[1-5]?[0-9]")) {
This matches any number of repetitions (including zero) of "a number followed by spaces", followed by a single repetition without a space. The "\\s" means "any whitespace character", the "+" after it means "one or more of what precedes", and the "*" means "zero more of what precedes" - which in this case is the term in parentheses.
Oh I see what you are trying to do
This is what you want
Scanner scan = new Scanner(System.in);
String allNums = "";
for(int i =0; i<6; i++)
{
System.out.println("Enter your number between 1-59");
int num = scan.nextInt();//Take the number in as an int
if(num >0 && num < 59)//Check if it is in range
{
allNums += num + " ";//if it is add it to a string
}
else
{
System.out.println("Number not in range");
i--;//go back one iteration if its not in range
}
}
System.out.println("Ticket printed £2. Your numbers are " + allNums);
I'm trying to use simply a while loop and the String method indexOf() to count how many times a certain word appears in a String given by the user.
The method I created seems to count how many times a certain letter appears, but not how many times a word appears. I think this is because the indexOf can't differentiate between a set of letters and spaces. So would I have to create another loop for the computer to understand what I consider words?
This is what I have so far:
public static void countWord(String sentenceEntered, String badWord){
int number = sentenceEntered.toUpperCase().indexOf(badWord, 0);
while (number >= 0){
System.out.print(number);
number = sentenceEntered.indexOf(badWord, number + 1);
}
}//end of countWord
But when I run my program, nothing gets printed.
There are two main problems in your method:
The code converts the sentence to uppercase, but not the input word. Also in the while loop, the sentence is not converted to uppercase. So there is an inconsistency here.
The offset in the call to String#indexOf should be the previous index plus the length of the word, i.e. number + badWord.length() instead of number + 1.
number = sentenceEntered.indexOf(badWord, number + badWord.length());
So after the changes, and assuming you no conversion to uppercase is done, the method should be as follows:
public static void countWord(String sentenceEntered, String badWord) {
int number = sentenceEntered.indexOf(badWord, 0);
while (number >= 0) {
System.out.println(number);
number = sentenceEntered.indexOf(badWord, number + badWord.length());
}
}// end of countWord
You should uppercase your badword too.
int number = sentenceEntered.toUpperCase().indexOf(badWord.toUpperCase(), 0);
Your problem is that you need to set the new starting index to number + badWord.length() in the while loop so that you can start the next indexOf at the end of the last badWord.
number = sentenceEntered.indexOf(badWord, number + badWord.length());
Apart from what is causing your current issue, if you just want to count the number of word occurence in a sentence, there's a simple way without using the loops
int length = sentenceEntered.length();
int badwordLength = badword.length();
if (sentenceEntered.contains(badword)) {
System.out.println("badword count: " + (length - sentenceEntered.replace(badword,"").length() / badwordLength));
}
I am working on a program that takes in 2 numbers and either adds, subtracts, multiplies, or divides them. The numbers can either be binary, hexadecimal, octal, or decimal numbers. In order for a user of the program to enter a binary number they must enter a "0b" in front of it. They would need to enter a "0" in front of the octal numbers, "0x" for hexadecimal, and just the number for a decimal number. The original number that is read in is a String then the method below converts it to an int or a double. I am trying to find out a solution if a user enters in a "0" for the number itself. Is there a way to see if the users input is only a 0 with nothing preceding it or following it? Here is my method I am working with. Any help would be awesome!
public double evaluate()
{
int sign = getSign();//makes number positive or negative
if (getOperand().startsWith("0"))// this is where i am trying to see if the number is just 0
{
return 0 * sign;
}
else if (getOperand().startsWith("0x"))
{
String op = getOperand().substring(2);
return Integer.parseInt(op, 16)*sign;
}
else if (getOperand().startsWith("0b"))
{
String op = getOperand().substring(2);
return Integer.parseInt(op, 2)*sign;
}
else if (getOperand().startsWith("0"))
{
String op = getOperand().substring(1);
return Integer.parseInt(op, 8)*sign;
}
else
{
String op = getOperand();
return (Double.parseDouble(op)) * sign;
}
You're making more work for yourself than you need. Just parse plain "0" as an octal number -- the result is still 0. No need to remove the leading zero.
That is,
// ...
else if (getOperand().startsWith("0"))
{
String op = getOperand();
return Integer.parseInt(op, 8)*sign;
}
// ...
Simply do this:
getOperand().equals("0")
If you want to check that the string is actually a zero with nothing before or after, you want equality.
getOperand().equals("0")
You can always check
if (getOperand().equals("0"))
However, you have another problem - your code will throw exceptions for invalid inputs. If the user enters just the prefix "0b" or "0x", your substring call would throw an exception. You must verify that there's at least one character following the prefix. It will also throw an exception if the substring can't be parsed as a number of the requested base.
Let's say I have this String
String myText="I think that stackoverflow is a very great website";
If i want to divide it in 2 lines i would have something like
I think that stackoverflow
is a very great website.
So the String will be now ("I think that stackoverflow\nis a very great website"
If I want it to divide in 3 lines it will be like
I think that
stackoverflow is a
very great website
What I've tried was just dividing the text, every line would have total number of words / n (n is the number of lines that i want to divide my text).
But this is a bad thing, i would have a result like
String myText="I me him is veryverylong wordvery longestwordever thisisevenlonger"
And the result would be (if i want to divide it in 2 lines) something like
"i you me is\nveryverylong wordvery longestwordever thisisevenlonger"
What do you guys suggest for me to try?
I've tried the common apache algorithm
http://pastebin.com/68zycavf
But my output text will be every word separated by \n ..if i use wrap(text,2)..
As Eran noted in his answer, you want to split at approximately the line length divided by the desired number of lines, but have to adjust for that being in the middle of a word.
I think his solution won't quite always give the best solution though, as it might sometimes be best to split before the word instead of after as he's doing.
A divide-and-conquer approach would be a recursive algorithm roughly as follows:
Let N be the desired number of lines and LENGTH be the number of characters in the input string (normalizing to single-spaces first).
If the character at LENGTH/N is a space, make the first cut there, and recursively call to split the remainder into N-1 lines, otherwise find the spaces at each end of the word containing this character and make trial cuts at both points with recursive calls again tom complete both cuts. Score the results somehow and choose the better.
I have implemented this as follows. For the scoring function, I chose to minimize the maximum length of lines in the split. A more complex scoring function might possibly improve the results, but this seems to work for all your cases.
public class WordWrapper {
public String wrapWords(String input, int lines) {
return splitWords(input.replaceAll("\\s+", " "), lines);
}
private String splitWords(String input, int lines) {
if (lines <= 1) {
return input;
}
int splitPointHigh = findSplit(input, lines, 1);
String splitHigh = input.substring(0, splitPointHigh).trim() + "\n" + splitWords(input.substring(splitPointHigh).trim(), lines - 1);
int splitPointLow = findSplit(input, lines, -1);
String splitLow = input.substring(0, splitPointLow).trim() + "\n" + splitWords(input.substring(splitPointLow).trim(), lines - 1);
if (maxLineLength(splitLow) < maxLineLength(splitHigh))
return splitLow;
else return splitHigh;
}
private int maxLineLength(String split) {
return maxLength(split.split("\n"));
}
private int maxLength(String[] lines) {
int maxLength = 0;
for (String line: lines) {
if (line.length() > maxLength)
maxLength = line.length();
}
return maxLength;
}
private int findSplit(String input, int lines, int dir) {
int result = input.length() / lines;
while (input.charAt(result) != ' ')
result+= dir;
return result;
}
}
I didn't actually bother with the special case of the lucky situation of the simple split landing on a space, and adding special handling for that might make it a little quicker. This code will in that case generate two identical "trial splits" and "choose one".
You might want to make all these methods static of course, and the recursion might give you a stack overflow for large inputs and large line counts.
I make no claim that this is the best algorithm, but it seems to work.
You can split based on the number of characters divided by n.
Then, for each line, you should add the end of the last word (which is the beginning of the next line, if the current line doesn't end with a space and the next line doesn't begin with a space), so that no words are split in the middle.
So if you have :
I me him is veryverylong wordvery longestwordever thisisevenlonger
And you wish to split it to two lines, you get :
I me him is veryverylong wordvery
longestwordever thisisevenlonger
In this case the second line already starts with a space, so we know that no word was split in the middle, and we are done.
If you split it to three lines, you first get :
I me him is veryverylo
ng wordvery longestwor
dever thisisevenlonger
Here some words were split, so you move "ng" to the first line, and then move "dever" to the second line.
I me him is veryverylong
wordvery longestwordever
thisisevenlonger
This is my solution using the split() function.
public class Textcut {
public static void main(String arg[]) {
String myText="I think that stackoverflow is a very great website";
int n = 2;
String[] textSplit = myText.split(" ");
int wordNumber = textSplit.length;
int cutIndex = wordNumber/n;
int i = cutIndex;
int j = 0;
while(i <= wordNumber) {
for(; j < i; j++) {
System.out.print(textSplit[j] + " ");
}
System.out.println("\n");
i = i+cutIndex;
}
}
}
I'm running a java program I created that stores data inputted by user. Specifically 4 array lists which are songName, songArtist, songYear & songAlbum.
I have a user input for "songYear" and I only want the program to accept a maximum of 4 digits in length and give an error otherwise, how can this be achieved?
here's the code I have for my add entry method:
public void addEntry(){
String newName = ui.getString("Enter the name of the track");
songName.add(newName);
String newArtist = ui.getString("Who performs this track");
songArtist.add(newArtist);
String newAlbum = ui.getString("What album is this track from");
songAlbum.add(newAlbum);
System.out.print("What year was the track released? ");
int newYear=input.nextInt(4);
songYear.add(newYear);
System.out.println("\n" + "Thank you, " +songName.get(songName.size()-1) + " has been added to the library.");
System.out.println("\n" + "Press 2 to view your library." + "\n");
}
You can use regex like: ^.{4}$
Means only if user typed 4 digits - return true, otherwise return false
To be sure that user used 4 numbers YYYY use something like:
^(?=[1-9]+)\d{4}$
Makes sure the year is 1 or 2 followed by three numbers; valid ranges in this case would be 1000-2999
^(?=[1-2][0-9]+)\d{4}$
Finally your code should be like:
if(inputUserStr.matches("^(?=[1-2][0-9]+)\d{4}$")){
// do some stuff
}
else{
// print error about valid input form [YYYY]
}
Depends entirely on the language but some approaches are:
check the string input using a len function; or
convert it to an integer and ensure it's less than 10,000; or
a regular expression like ^\d{1,4}.
No doubt there'll be other validation checks such as ensuring string input is all-numeric, and you're not trying to input a song that was written twenty years in the future, but they're added checks you should consider.
1) Accept the user's input and using the substring method, save only the first four characters (Specify to user that first 4 characters are considered).
2) You can ask the user to reenter the value if it is not 4 characters:
Scanner sc = new Scanner(System.in);
String a = sc.next();
if (a.matches("...."))
{
System.out.print(a);
}
else
{
System.out.print("Input again:" );
a = sc.next();
}
I've written a sample regex for 4 characters. But you can always change it.