java - Testing For Two Pairs In a Given String -
java - Testing For Two Pairs In a Given String -
you user input of string of 4 positive integers ex "0 1 1 2" test see if there 2 pairs nowadays in string. integerpairs object have constructor takes no arguments , next 2 methods:
public void setpairstring(string str) - method take string argument , store in instance variable.
public boolean hastwopairs() - homecoming true when there 2 pairs nowadays in string.
you can utilize scanner on string system.in. works same way constructor takes string rather system.in.
after done scanner sure phone call {your scaner variable name}.close(); don't afraid create local variables. can utilize them hold numbers string.
that project , horribly lost.... don't want me, of course, love have little more instruction on this. code far: (not much @ , have been sitting here stumped 3-4 hours straight)
my class:
public class integerpairs { private string str; public integerpairs(string pairs){ str = pairs; } public void setpairstring(string str){ this.str = str; } public boolean hastwopairs(){ no thought boolean method. assuming have loop somewhere????
haven't gotten main class because need figure out first.
edit
this more looking for:
public class integerpairs {
private string thestring; public integerpairs() { } public void setpairstring(string str) { thestring = str; } public boolean hastwopairs() { string = thestring.substring(0, 1); string b = thestring.substring(1, 2); string c = thestring.substring(2, 3); string d = thestring.substring(3, 4); boolean istheretwopairs = (a.equals(b) && c.equals(d)) ? true : ((a + b).equals(c + d)) ? true : false; homecoming istheretwopairs; } }
then main class:
import java.util.*;
public class main {
public static void main(string[] args) { integerpairs str = new integerpairs(); scanner keyboard = new scanner(system.in); system.out.print("please come in 4 numbers:"); string pairin = keyboard.nextline(); keyboard.close(); str.setpairstring(pairin); boolean istheretwopairs = str.hastwopairs(); string whattoprint = (istheretwopairs == true) ? "there 2 pairs" : "there not 2 pairs"; system.out.print(whattoprint); } }
took me while got it. next question is: running junit test it's telling me "newly constructed integerpairs object should not have 2 pairs.", ip.hastwopairs());
not quite sure means because constructor has no assignment.
what have utilize loop , if statement to compare every character every other character in string , use counter when pair found if counter not equal two, boolean should made homecoming false. boolean method should check counter that's all.
java string loops boolean pair
Comments
Post a Comment