Question

I am working on java patter. my code read the string and it will get only...

I am working on java patter. my code read the string and it will get only the numbers. in this case,124,140,130..but i want to get the maximum number. which is 140. I would appreciate any kind of help.

This is my code

public class questions {

   public static void main(String[] args) {
       int total=0;

       String test="it tookthismuch total time: 124. it tookthismuch total time: 140. it tookthismuch total time: 130. ";
      
       Pattern pat = Pattern.compile("(it tookthismuch total time:) (.+?) ");
       Matcher pro= pat.matcher(test);
       while (pro.find()) {
           total = Integer.parseInt(pro.group(2).replaceAll("\\D+", ""));      
           System.out.println(total);              
       }  
   }

}

The output

124
140
130

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

CODE

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Questions
{
   public static void main(String[] args)
   {
       int i = 0;
      
       //creating an array to store values
       int array[] = new int[10];
      
       //initializing String test
       String test="it tookthismuch total time: 124. it tookthismuch total time: 140. it tookthismuch total time: 130. ";
      
       //creating a pattern instance
       Pattern pat = Pattern.compile("(it tookthismuch total time:) (.+?) ");
      
       //finding matches of the pattern in the test
       Matcher pro= pat.matcher(test);
      
       //loop to find occurrences of pattern in test
       while (pro.find())
       {
           //storing integer in array
           array[i] = Integer.parseInt(pro.group(2).replaceAll("\\D+", ""));
           i++;
       }
      
       //initializing max
       int max = array[0];
      
       //loop to find max
       for(i = 0; i < array.length; i++)
       {
           //if max is less than current element
           if(max < array[i])
               max = array[i];
       }
      
       //printing max
       System.out.println(max);
   }
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
I am working on java patter. my code read the string and it will get only...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Java Help Please: I'm doing something wrong with my while loop. I want my code to...

    Java Help Please: I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make sure it matches. If it doesn't I want it to print out Error! and I want my While loop to ask the user to try again by inputting another phone number. When the user puts in a phone number that matches I want the program to continue to output my tokens. When I run this...

  • I am having a problem with my code. Here are the relevant parts. The method priceNow...

    I am having a problem with my code. Here are the relevant parts. The method priceNow is supposed to take as an input a string in the form "Q1'20" (for example) and calculate the new price knowing that the price of the CPU decreases by 2% every quarter. Here is my class definition code: public class CPUv2 {    private String launchDate;    public CPUv2() {        this.CPUGeneration = 1;        this.CPUSeries = "i3";        this.suggestedPrice =...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • I am trying to run a couple of functions in my main and It won't output...

    I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code. package a3; import java.util.Scanner; public class LoopsAndImages { public static void main(String[] args){ String test = "A rabbit has a carrot"; Boolean numbers = hasMoreEvenThanOdd("12344"); System.out.println(test); System.out.println(hideLetterA(test)+" This is the...

  • Java Branches code not working please HELP!! I am trying to build this class that will...

    Java Branches code not working please HELP!! I am trying to build this class that will ask for an ingredient, and the number of cups of the ingredient. It should then branch into an if/else branch off to verify that the number entered is valid and that it is within range. Once that branch completes it should then continue on to ask for the calories per cup, and then calculate total calories. Once I get it to enter the branch...

  • Written in Java I have an error in the accountFormCheck block can i get help to...

    Written in Java I have an error in the accountFormCheck block can i get help to fix it please. Java code is below. I keep getting an exception error when debugging it as well Collector.java package userCreation; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import javafx.event.ActionEvent; import javafx.fxml.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.text.Text; import javafx.stage.*; public class Controller implements Initializable{ @FXML private PasswordField Password_text; @FXML private PasswordField Confirm_text; @FXML private TextField FirstName_text; @FXML private TextField LastName_text;...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • Creating a calculator for my CS1 class and my code was able to run but it...

    Creating a calculator for my CS1 class and my code was able to run but it crashes near the end. As far as I know, the error means that it's retrieving "null" from the variable but I haven't seen that kind of error from this kind of situation. I would appreciate any help. I'm just starting up in Java so I apologize if this is a silly question. Thank you! ``` ----jGRASP exec: java CS1Calculator ------------ Welcome to the CS1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT