Is it possible to use if statement for string limit?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1268 Views - Last Post: 16 October 2012 - 01:37 PM Rate Topic: -----

#1 AlexaM160  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Is it possible to use if statement for string limit?

Posted 16 October 2012 - 10:48 AM

Hey, I imagine there's probably been a couple of people around here with the same problem, but anyways.

I have a program that separates a 5 digit number into it's 5 separate digits (IE input 5 digit number and it separates them).

What I've done is made it so that the number is read as a string, then separated into characters and then the numerical value is read.

What I'm looking to do is find a way to create something like an if statement, where if the string exceeds 5 characters, or is under 5 characters it comes back with an error and will ask again. Here's my code so far :

import java.lang.*;
import java.util.Scanner;
	 
	public class Assign1 {
	 
	    public static void main(String[] args) {
	 
	        Scanner NumberSplit = new Scanner(System.in);
	        System.out.print("Enter a 5 digit number: ");
	        String input = NumberSplit.nextLine();  //reading input number as string instead of as int
	         
	        char c1 = input.charAt(0); //Allows to separate into characters
	        char c2 = input.charAt(1);
	        char c3 = input.charAt(2);
	        char c4 = input.charAt(3);
	        char c5 = input.charAt(4);
	        int digit1 = Character.getNumericValue(c1); //Gets the numerical value from character
	        int digit2 = Character.getNumericValue(c2); 
	        int digit3 = Character.getNumericValue(c3); 
	        int digit4 = Character.getNumericValue(c4); 
	        int digit5 = Character.getNumericValue(c5); 
	         
	        
		System.out.println("The number is");
		System.out.println(digit1); //Prints result
        	System.out.println(digit2);
	        System.out.println(digit3);
	        System.out.println(digit4);
	        System.out.println(digit5);
	    }
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to use if statement for string limit?

#2 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:06 AM

I would suggest putting the part where you read the string into a do while loop, checking to see if the string is 5 characters long.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:26 AM

String has a length() method which returns the number of characters in the String. This might be useful.

This would be easier if you used an int array for the digits. This way you could state your digit extraction logic once and have it work as many times as you wanted, in a loop. It would also be easier to change the program if the requirements change - say you need to extract a ten-digit number, it would be a bit less of a nuisance to make that change.

Of course, if you only want to print out the information, you don't even need the array, just extract and print (again in a loop)
Was This Post Helpful? 0
  • +
  • -

#4 AlexaM160  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:35 AM

I'm not really familiar with do while loops, but I could give that shot.

I know I'd want something like

While stringInput = 5 characters
Do so and so (print out numbers)

but how would I portray that in code. Also would if something like "if stringInput is greater than 5 characters or less than 5 characters return to start" not work?

Also the other issue is I'm not entirely sure how to write this in actual Java code.

My other idea would to change my code completely. Instead of reading as a string, change it to integer. Then I could use a condition where I could just if a number is higher than 99999 or lower than 10000, right?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:39 AM

View PostAlexaM160, on 16 October 2012 - 01:35 PM, said:

I know I'd want something like

While stringInput = 5 characters
Do so and so (print out numbers)


No, you wouldn't want a while loop for that, unless you expect the length of the String to change. (you'd either skip the loop, or enter an endless loop)

Use an if and the String.length() method I've already suggested.

Quote

My other idea would to change my code completely. Instead of reading as a string, change it to integer. Then I could use a condition where I could just if a number is higher than 99999 or lower than 10000, right?


This is a good thought, but what if someone enters "00212"? Zero is a digit, but it's lost on conversion to int.
Was This Post Helpful? 0
  • +
  • -

#6 AlexaM160  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:49 AM

Ok, also that's a very good point about 00212 being a 5 digit number still, didn't think of that. I think I know what I need to do, although I have one more question.

I looked up string length and looked at some examples and things. My question is how does it measure a string, just by characters?

For instance if I had string input = "hello"; and I asked it to give me the length of the string, does it measure it and return the value as characters?
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:53 AM

View PostAlexaM160, on 16 October 2012 - 06:49 PM, said:

Ok, also that's a very good point about 00212 being a 5 digit number still, didn't think of that. I think I know what I need to do, although I have one more question.

I looked up string length and looked at some examples and things. My question is how does it measure a string, just by characters?

For instance if I had string input = "hello"; and I asked it to give me the length of the string, does it measure it and return the value as characters?


and return the value as characters

huh? no it gives you the length of the String as an int
It counts the characters in the String
It is not hard to imagine the String class having a char[] with the characters
Was This Post Helpful? 0
  • +
  • -

#8 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:53 AM

I was suggesting:
do {
System.out.print("Enter a 5 digit number: "); 
String input = NumberSplit.nextLine();} while(input.length() != 5); 

or something like that.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 11:59 AM

View PostAlexaM160, on 16 October 2012 - 01:49 PM, said:

I looked up string length and looked at some examples and things. My question is how does it measure a string, just by characters?

For instance if I had string input = "hello"; and I asked it to give me the length of the string, does it measure it and return the value as characters?


If you're curious, you can look for yourself!


View PostCasiOo, on 16 October 2012 - 01:53 PM, said:

It is not hard to imagine the String class having a char[] with the characters


I would hope not. :)

View PostZoquo, on 16 October 2012 - 01:53 PM, said:

I was suggesting:
do {
System.out.print("Enter a 5 digit number: "); 
String input = NumberSplit.nextLine();} while(input.length() != 5); 

or something like that.



Okay, so you're on to input validation. Yes, that's going to ensure that the length of the String is 5 when it does get out of the loop.

This post has been edited by jon.kiparsky: 16 October 2012 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 12:00 PM

View Postjon.kiparsky, on 16 October 2012 - 06:59 PM, said:

View PostCasiOo, on 16 October 2012 - 01:53 PM, said:

It is not hard to imagine the String class having a char[] with the characters


I would hope not. :)


Why don't you hope for that ^^ ?
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 12:04 PM

View PostCasiOo, on 16 October 2012 - 02:00 PM, said:

View Postjon.kiparsky, on 16 October 2012 - 06:59 PM, said:

View PostCasiOo, on 16 October 2012 - 01:53 PM, said:

It is not hard to imagine the String class having a char[] with the characters


I would hope not. :)


Why don't you hope for that ^^ ?



I would hope that it's not hard. Since (as you know) that's actually what it does have. :)
Was This Post Helpful? 1
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 12:08 PM

View Postjon.kiparsky, on 16 October 2012 - 07:04 PM, said:

View PostCasiOo, on 16 October 2012 - 02:00 PM, said:

View Postjon.kiparsky, on 16 October 2012 - 06:59 PM, said:

View PostCasiOo, on 16 October 2012 - 01:53 PM, said:

It is not hard to imagine the String class having a char[] with the characters


I would hope not. :)


Why don't you hope for that ^^ ?



I would hope that it's not hard. Since (as you know) that's actually what it does have. :)



ah :D
Was This Post Helpful? 0
  • +
  • -

#13 AlexaM160  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 12:13 PM

Would Zoquo's do while suggestion be ideal in this situation? I like it considering I'm pretty sure I understand it. While the string isn't equal to five characters, it'll keep looping and asking for a 5 character input?
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 12:21 PM

Yes. It's basically the same as my suggestion, only better in that if the input is not good, it repeats the request.
Was This Post Helpful? 0
  • +
  • -

#15 AlexaM160  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Re: Is it possible to use if statement for string limit?

Posted 16 October 2012 - 12:32 PM

Alright, well I've honestly never written a loop, but as of right now here's my updated code:

import java.lang.*;
import java.util.Scanner;
	 
	public class Assign1 {
	 
	    public static void main(String[] args) {
	 

		
	        Scanner NumberSplit = new Scanner(System.in);
	        do {
		System.out.print("Enter a 5 digit number: ");
	        String input = NumberSplit.nextLine();  
								 } 
		while (input.length() != 5);  
	       

		char c1 = input.charAt(0); //Allows to separate into characters
	        char c2 = input.charAt(1);
	        char c3 = input.charAt(2);
	        char c4 = input.charAt(3);
	        char c5 = input.charAt(4);
	        int digit1 = Character.getNumericValue(c1); //Gets the numerical value from character
	        int digit2 = Character.getNumericValue(c2); 
	        int digit3 = Character.getNumericValue(c3); 
	        int digit4 = Character.getNumericValue(c4); 
	        int digit5 = Character.getNumericValue(c5); 
	         
	        
		System.out.println("The number is");
		System.out.println(digit1); //Prints result
        	System.out.println(digit2);
	        System.out.println(digit3);
	        System.out.println(digit4);
	        System.out.println(digit5);
	

	}

}


I am currently getting 6 errors:

Quote

Assign1.java:15: error: cannot find symbol
while (input.length() != 5);
^
symbol: variable input
location: class Assign1
Assign1.java:18: error: cannot find symbol
char c1 = input.charAt(0); //Allows to separate into characters
^
symbol: variable input
location: class Assign1
Assign1.java:19: error: cannot find symbol
char c2 = input.charAt(1);
^
symbol: variable input
location: class Assign1
Assign1.java:20: error: cannot find symbol
char c3 = input.charAt(2);
^
symbol: variable input
location: class Assign1
Assign1.java:21: error: cannot find symbol
char c4 = input.charAt(3);
^
symbol: variable input
location: class Assign1
Assign1.java:22: error: cannot find symbol
char c5 = input.charAt(4);
^
symbol: variable input
location: class Assign1
6 errors


Is this because I wrote the loop wrong or wrote it in the wrong place? Looking at what Zoquo wrote, I figured putting it right before the prompt for input and then before the rest was right.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2