• 0

[JAVA] Need help reading a long string


Question

Ok, I have to try to read a 10 digit string, then if it is not 10 digits, tell the user it is counterfeit. Then, if it is 10 digits, go through a sequence of things.

Basically something like this:

- initialize vars

- Read the check number

- If not 10 digits, counterfeit.

Else,

for each char in the number {

if (char is zero) zeroCount ++; reset nonZeroCount;

else nonZeroCount++; reset zeroCount;

if (either of the counts exceeds limit) counterfeit! and get out;

}

If (get out prematurely) counterfeit;

Else, OK.

In the 10-digit check number, if there are (a) three or more zeros in succession, and/or (b) four or more non-zero in succession, then the check should be considered counterfeit

So, here's the code I have so far:

import java.util.*;
public class ValidateCheck {

	public static void main(String[] args) {
	System.out.println("Enter a 10-digit check number:");                 
  Scanner sc = new Scanner (System.in); 
  String number = sc.next();
  int zeroCount = 0, nonZeroCount = 0;
  
  int index = number.length();
  
  if(index!=10) 
  {
    System.out.println("Counterfeit! See Supervisor.");
  } 
  else 
  {
    for(int i=0;i<index;i++)
    {
    if(number.charAt(i)==0)
       {
     	 zeroCount++;
     	 nonZeroCount = 0;
       } 
    else 
       {
     	 nonZeroCount++; 
     	 zeroCount = 0;
       }
    if (zeroCount >= 3 || nonZeroCount >= 4)
       {
      System.out.println("Counterfeit! See Supervisor.");
     	 break;
       } 
    else
    {
   	 System.out.println("Check Good!");
   	 break;
    }
   }
  }
  }
}

Any thoughts would be much appreciated.

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Well, it seems even if I enter 0003243012

Which has 3 zeros in succession, I still get Check Good when it isn't.

Or I have 4 or more non-zeros in succession.

Link to comment
Share on other sites

  • 0

I think I solved it. Real Simple, you are breaking when you shouldn't be breaking.

As a hint, use a line like this

System.out.println(nonZeroCount + " " + zeroCount + " index = " + i);

...in your conditional statements. This could be better written, but you get the idea. I'm checking your counts and the index of the loop. This should give you the answer. I think this is your only problem, but I'll leave the testing to you. It seemed to at least clear up part of the problem and I think it's the issue.

Link to comment
Share on other sites

  • 0

Look at your second if/else construct...You are checking on the sequences of numbers...ok....Your if is not going to fall through on the first number....so what do you do...you go to the else...follow me now?

Link to comment
Share on other sites

  • 0

import java.util.*;
public class ValidateCheck {

public static void main(String[] args) {
System.out.println("Enter a 10-digit check number:");                 
 Scanner sc = new Scanner (System.in); 
 String number = sc.next();
 int zeroCount = 0, nonZeroCount = 0;

 int index = number.length();

 if(index!=10) 
 {
   System.out.println("Counterfeit! See Supervisor.");
   break;
 } 
 else 
 {
   for(int i=0;i<index;i++)
   {
   if(number.charAt(i)==0)
      {
      zeroCount++;
      nonZeroCount = 0;
      } 
   else 
      {
      nonZeroCount++; 
      zeroCount = 0;
      }
   if (zeroCount >= 3 || nonZeroCount >= 4)
      {
     System.out.println("Counterfeit! See Supervisor.");
      break;
      } 
   else
   {
    System.out.println("Check Good!");

   }
  }
 }
 }
}

Now, I understand that in the second if statement with the system. out.println(check good) is gonna run every time, but I can't seem to get the damn thing to use continue or break when I want it to.

Link to comment
Share on other sites

  • 0

I had that, but then the program wouldn't compile because it said the very first loop with the break statement was outside the loop.

Rather, that the break was outside the loop. No clue what that means because it's in the IF statement.

Edited by LRoling
Link to comment
Share on other sites

  • 0
I had that, but then the program wouldn't compile because it said the very first loop with the break statement was outside the loop.

Rather, that the break was outside the loop. No clue what that means because it's in the IF statement.

584735587[/snapback]

In the first condition of the top if-statement, you have a break. You don't need it as anything in the else-statement will not be executed anyhow.

Link to comment
Share on other sites

  • 0

import java.util.*;
public class validatecheck {

	public static void main(String[] args) {
  System.out.println("Enter a 10-digit check number:");                 
  Scanner sc = new Scanner (System.in); 
  String number = sc.next();
  int zeroCount = 0, nonZeroCount = 0;
  
  int index = number.length();
  
  if(index!=10) 
  {
    System.out.println("Counterfeit! See Supervisor.");
    break;
  } 
  else 
  {
  
  for(int i=0;i<index;i++)
  {
    
 	 if(number.charAt(i)==0)
       {
     	 zeroCount++;
     	 nonZeroCount = 0;
       } 
    else 
       {
     	 nonZeroCount++; 
     	 zeroCount = 0;
       }
    
    if (zeroCount >= 3 || nonZeroCount >= 4)
       {
     	 System.out.println("Counterfeit! See Supervisor.");
     	 break;
       }
    }
  }
  System.out.println("Check Good!");
	}
}

Shows an error on the first break saying it's outside the loop

:blink:

Link to comment
Share on other sites

  • 0
import java.util.*;
public class validatecheck {

	public static void main(String[] args) {
  System.out.println("Enter a 10-digit check number:");                 
  Scanner sc = new Scanner (System.in); 
  String number = sc.next();
  int zeroCount = 0, nonZeroCount = 0;
  
  int index = number.length();
  
  if(index!=10) 
  {
    System.out.println("Counterfeit! See Supervisor.");
    break;
  } 
  else 
  {
  
  for(int i=0;i<index;i++)
  {
    
 	 if(number.charAt(i)==0)
       {
     	 zeroCount++;
     	 nonZeroCount = 0;
       } 
    else 
       {
     	 nonZeroCount++; 
     	 zeroCount = 0;
       }
    
    if (zeroCount >= 3 || nonZeroCount >= 4)
       {
     	 System.out.println("Counterfeit! See Supervisor.");
     	 break;
       }
    }
  }
  System.out.println("Check Good!");
	}
}

Shows an error on the first break saying it's outside the loop

:blink:

584735755[/snapback]

See the top IF statement... in your first condition, the failure, it has a break. Remove it.

Link to comment
Share on other sites

  • 0

OK, then I entered this:

Enter a 10-digit check number:

0012300123

Counterfeit! See Supervisor.

Check Good!

I just want one output, not both.

Link to comment
Share on other sites

  • 0
OK, then I entered this:

Enter a 10-digit check number:

0012300123

Counterfeit! See Supervisor.

Check Good!

I just want one output, not both.

584735790[/snapback]

pull this

   
if (zeroCount >= 3 || nonZeroCount >= 4)
      {
      System.out.println("Counterfeit! See Supervisor.");
      break;
      }

out of the loop and remove the break and add an else that contains the "Check Good" message.

Link to comment
Share on other sites

  • 0

import java.util.*;
public class validatecheck {

	public static void main(String[] args) {
  System.out.println("Enter a 10-digit check number:");                 
  Scanner sc = new Scanner (System.in); 
  String number = sc.next();
  int zeroCount = 0, nonZeroCount = 0;
  
  int index = number.length();
  
  if(index!=10) 
  {
    System.out.println("Counterfeit! See Supervisor.");
  } 
  else 
  {
  
  for(int i=0;i<index;i++)
  {
    
 	 if(number.charAt(i)==0)
       {
     	 zeroCount++;
     	 nonZeroCount = 0;
       } 
    else 
       {
     	 nonZeroCount++; 
     	 zeroCount = 0;
       }
   }
  }
 	 if (zeroCount >= 3 || nonZeroCount >= 4)
      {
      System.out.println("Counterfeit! See Supervisor.");
      } else {
       System.out.println("Check Good!");
      }
	}
}

Like so?

Still does some wierd stuff:

Output:

Enter a 10-digit check number:

0012300123

Counterfeit! See Supervisor.

Not 3 zeros in a row or 4 non-zeros, not sure why it counterfeits

Link to comment
Share on other sites

  • 0
import java.util.*;
public class validatecheck {

	public static void main(String[] args) {
  System.out.println("Enter a 10-digit check number:");                 
  Scanner sc = new Scanner (System.in); 
  String number = sc.next();
  int zeroCount = 0, nonZeroCount = 0;
  
  int index = number.length();
  
  if(index!=10) 
  {
    System.out.println("Counterfeit! See Supervisor.");
  } 
  else 
  {
  
  for(int i=0;i<index;i++)
  {
    
 	 if(number.charAt(i)==0)
       {
     	 zeroCount++;
     	 nonZeroCount = 0;
       } 
    else 
       {
     	 nonZeroCount++; 
     	 zeroCount = 0;
       }
   }
  }
 	 if (zeroCount >= 3 || nonZeroCount >= 4)
      {
      System.out.println("Counterfeit! See Supervisor.");
      } else {
       System.out.println("Check Good!");
      }
	}
}

Like so?

Still does some wierd stuff:

Output: 

Enter a 10-digit check number:

0012300123

Counterfeit! See Supervisor.

Not 3 zeros in a row or 4 non-zeros, not sure why it counterfeits

584735837[/snapback]

It's been a while since I've done Java, but can you compare a char to an int without conversion?

I would try testing the charAt(i) like so:

if( Character.digit(number.charAt(i), 10) == 0 )
{
    // etc...
}
else
{
    // etc...
}

Link to comment
Share on other sites

  • 0

I just did this and it works:

import java.util.*;
public class validatecheck {

	public static void main(String[] args) {
  System.out.println("Enter a 10-digit check number:");                 
  Scanner sc = new Scanner (System.in); 
  String number = sc.next();
  char[] c = number.toCharArray();
  int zeroCount = 0, nonZeroCount = 0;
  
  int index = number.length();
  System.out.println("" + number.length());
 	 
  if(index!=10) 
  {
    System.out.println("Counterfeit! See Supervisor.");
    System.out.println("First Loop");
  } 
  else 
  {
  
  for(int i=0;i<c.length - 1;i++)
  {
    
 	 if(c[i]=='0')
       {
     	 zeroCount++;
     	 nonZeroCount = 0;
       } 
    else 
       {
     	 nonZeroCount++; 
     	 zeroCount = 0;
       }
   }
  }
  System.out.println("" + zeroCount);
  System.out.println("" + nonZeroCount);
 	 if (zeroCount >= 3 || nonZeroCount >= 4)
      {
      System.out.println("Counterfeit! See Supervisor.");
      } else {
       System.out.println("Check Good!");
      }
	}
}

But the Character.digit works as well, thanks a lot guys.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.