Home > Mobile >  having some trouble with a while loop, and getting an if statement to execute
having some trouble with a while loop, and getting an if statement to execute

Time:12-02

This is an exercise we are doing in our last chapter of the book. I covered everything except the if statement that requires the input to follow a number after a comma.

For example: John Doe, nine

output should trigger, comma should follow integer. I am trying to get the if statement to at least recognize that if the input after the comma is not a number then the output if statement should trigger but I can't.

What is working at least is input statements with more than one comma.

The expected format should be:

Jane Doe, 9

while (!datapoint.equals("-1")) {
      


  
  System.out.println("Enter a data point (-1 to stop input):");
  datapoint = scnr.nextLine();
  
  
  int commaCount = 0;
  
  for( int i= 0; i < datapoint.length(); i  )
  {
      if(datapoint.charAt(i) == ',')
          commaCount  ;
  }
  
  if (commaCount > 1) {
      
      System.out.println("Error: Too many commas in input.");
      
  }
  
  if (datapoint.equals("-1")) {
      
      break;
      
  }
  
  if (!datapoint.contains(",")) {
      
      System.out.println("Error: No comma in string.");
      
  }


  
    
  
  if (datapoint.contains(",")) {
      
  String[] values = datapoint.split(",");     
  
  temp1 = values[0];
  temp2 = values[1];
  
  temp1 = temp1.replace(",", "");  // commma removed   ex/ Jane Austen
  temp2 = temp2.replace(" ", "");  // space removed    ex/ 6 
  
  String trigger = temp2;
  
  
  if (values.length == 2)  {
  
  
  if (temp2.length() > 2)   {    //if datapoint does not contain a number after commma
      
      
    System.out.println("Error: Comma not followed by an integer.");  
      
      
      
    }
  
  
  if (trigger.length() < 3) {
      
  int convert = Integer.parseInt(temp2);  // converts string to integer   
  
  System.out.println("Data string: "   temp1); 
  System.out.println("Data integer: "   convert);  // 2 
  
  names.add(temp1);
  ints.add(convert);
  
  }
  
  }
  
  }
       
  
  System.out.println();
  
  }

CodePudding user response:

Stop, back up and take a deep breath ... I seem to be saying that a lot lately.

Break the problem down and then, step by step, try and solve each problem, not try and do the whole thing, because your confused and I'm confused.

If we assume Jane Doe, 9 is a valid format, then we can start from there.

We can simply split the String on the comma doing something like...

String[] parts = value.split(",");

Based on the "valid" input, we would end up with ["Jane Doe", " 9"], sweet. So we can do a basic validation using something like...

if (parts.length == 2) {
    
} else {
    System.out.println("Invalid input, expecting [String],[int]");
}

Okay, sweet. Now we can valid the individual parts of the String

First, we should check to see if the name isn't blank. Then we should try and determine if the second part is an integer or not. Not, here's a neat trick I think people miss out on - you can use more then one Scanner and use it validate the String

  • Related