Connecting Tech Pros Worldwide Forums | Help | Site Map

Scanner class confusion

tolkienarda's Avatar
Needs Regular Fix
 
Join Date: Dec 2006
Posts: 316
#1: Sep 13 '07
hi all
i read the scanner class documentation from sun's website and i thought i would have some fun trying to write a program that stores a line from the user to var input and then parses it on white space then depending on what the new string is i will do somthing. i think there is an easier way than what i am trying but i am just trying to get these concepts down before i move on to my next step. i am getting an incompatiable types error in my loop condition
Expand|Select|Wrap|Line Numbers
  1.  import java.util.*;
  2. public class asg3 {
  3.     static Scanner console = new Scanner(System.in);
  4.     public static void main(String[] args) {
  5.         String[] arrnums = new String[20];
  6.         String input = " ";
  7.         Scanner parse = new Scanner(input).useDelimiter("\\s*");
  8.         int i = 0;
  9.         Random ran = new Random();
  10.         System.out.print("please enter a number, /n enter 'r' to insert a random number \n you can send a max int using the following syntax 'r(max int) \n enter 'sub' to submit data, enter a q to end program \n");
  11.         input = console.nextLine(); 
  12.         System.out.print(input);
  13.         while(arrnums[i] = parse.next())
  14.         {
  15.             System.out.println(i);
  16.             //and some other stuff
  17.             i++;
  18.         }
  19.  
  20.     }
  21. }
i am not even sure if what my while condition does is legal, i am basicialy saying to keep going until there is no more substrings in the string.

if you could point me toward a different manual or offer some of your own advice i would be grateful

eric

madhoriya22's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: India
Posts: 254
#2: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by tolkienarda

hi all
i read the scanner class documentation from sun's website and i thought i would have some fun trying to write a program that stores a line from the user to var input and then parses it on white space then depending on what the new string is i will do somthing. i think there is an easier way than what i am trying but i am just trying to get these concepts down before i move on to my next step. i am getting an incompatiable types error in my loop condition

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class asg3 {
  3. static Scanner console = new Scanner(System.in);
  4. public static void main(String[] args) {
  5.     String[] arrnums = new String[20];
  6.     String input = " ";
  7.     Scanner parse = new Scanner(input).useDelimiter("\\s*");
  8.     int i = 0;
  9.     Random ran = new Random();
  10.     System.out.print("please enter a number, /n enter 'r' to insert a random number \n you can send a max int using the following syntax 'r(max int) \n enter 'sub' to submit data, enter a q to end program \n");
  11.     input = console.nextLine(); 
  12.     System.out.print(input);
  13.     while(arrnums[i] = parse.next())
  14.     {
  15.         System.out.println(i);
  16.         //and some other stuff
  17.         i++;
  18.     }
  19.  
  20. }
  21. }
i am not even sure if what my while condition does is legal, i am basicialy saying to keep going until there is no more substrings in the string.

if you could point me toward a different manual or offer some of your own advice i would be grateful

eric

Hi,
The way you are comparing two strings
Expand|Select|Wrap|Line Numbers
  1.  
  2. while(arrnums[i] = parse.next())
  3.  
is not correct. You should compare them using equals() method of string like this ..
Expand|Select|Wrap|Line Numbers
  1.  
  2. while(arrnums[i].equals(parse.next()))
  3.  
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#3: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by madhoriya22

The way you are comparing two strings

Expand|Select|Wrap|Line Numbers
  1.  
  2. while(arrnums[i] = parse.next())
  3.  
is not correct.

Hi!

Even if you could compare Strings with the "equals"-symbol, there would be an error in your code: for comparison, always use == instead of =. The first is for comparing, the second for defining values. That's not what you want to do here, is it? Because that will not return a boolean.

Greetings,
Nepomuk
madhoriya22's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: India
Posts: 254
#4: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by nepomuk

Hi!

Even if you could compare Strings with the "equals"-symbol, there would be an error in your code: for comparison, always use == instead of =. The first is for comparing, the second for defining values. That's not what you want to do here, is it? Because that will not return a boolean.

Greetings,
Nepomuk

Hi,
Even using == for comparing strings is not a good idea. Sometimes it can give you errors also. Always use equals(String str) method to compare strings :)
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#5: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by madhoriya22

Hi,
Even using == for comparing strings is not a good idea. Sometimes it can give you errors also. Always use equals(String str) method to compare strings :)

In fact, it won't give you the result you want in most cases - if you define
Expand|Select|Wrap|Line Numbers
  1. String str1 = "Hello";
  2. String str2 = "Hell";
  3. str2 += "o";
  4. System.out.println(str1 == str2);
  5.  
the output will be "false", although both Strings have the value "Hello", as the Objects addresses are checked, instead of their values.
Expand|Select|Wrap|Line Numbers
  1. String str1 = "Hello";
  2. String str2 = str1;
  3. System.out.println(str1 == str2);
  4.  
will return "true".

Greetings,
Nepomuk
madhoriya22's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: India
Posts: 254
#6: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by nepomuk

In fact, it won't give you the result you want in most cases - if you define

Expand|Select|Wrap|Line Numbers
  1. String str1 = "Hello";
  2. String str2 = "Hell";
  3. str2 += "o";
  4. System.out.println(str1 == str2);
  5.  
the output will be "false", although both Strings have the value "Hello", as the Objects addresses are checked, instead of their values.
Expand|Select|Wrap|Line Numbers
  1. String str1 = "Hello";
  2. String str2 = str1;
  3. System.out.println(str1 == str2);
  4.  
will return "true".

Greetings,
Nepomuk

Hi,
Again nice description buddy :)
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#7: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by madhoriya22

Hi,
Again nice description buddy :)

And again, I just do my best! ^^
tolkienarda's Avatar
Needs Regular Fix
 
Join Date: Dec 2006
Posts: 316
#8: Sep 13 '07

re: Scanner class confusion


hi, in the loop condition i am trying to do an assignment, i am hoping to go keep going through my string using parse.next() and fill the array with the returned values. and when there are nor more values in my string for parse.next() to return false will be returned and i will be done with my loop. so i am not checking to see if my values are equal (the array is empty) but rather i am filling my array with as many values as it can hold.
in php i found this to be common logic but i have come to realize that php is a very simple language that lets me get away with about anything so if this isn't allowed in java is there another way to do it

thanks
eric
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by tolkienarda

hi, in the loop condition i am trying to do an assignment, i am hoping to go keep going through my string using parse.next() and fill the array with the returned values. and when there are nor more values in my string for parse.next() to return false will be returned and i will be done with my loop. so i am not checking to see if my values are equal (the array is empty) but rather i am filling my array with as many values as it can hold.
in php i found this to be common logic but i have come to realize that php is a very simple language that lets me get away with about anything so if this isn't allowed in java is there another way to do it

thanks
eric

Have you tried this on the compiler yet?
Does it work yet?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#10: Sep 13 '07

re: Scanner class confusion


Oh your loop condition is definitely invalid.
The while condition must be a boolean value. That statement that you put there does not return a boolean value but returns the a value of the type of the object being assigned.
tolkienarda's Avatar
Needs Regular Fix
 
Join Date: Dec 2006
Posts: 316
#11: Sep 13 '07

re: Scanner class confusion


ok thanks, darn php, gave me some bad habbits but it was so much fun. i have actualy found some new logic that works but i have a similar question. well not rely to similar but kinda
i need to check to see if a value in an array is convertable to an integer and if so store it and if not go to my next if condition.

so what i have for code is
Expand|Select|Wrap|Line Numbers
  1. if(arrnum[j].parseInt(arrstr[i]))
  2. {
  3.      j++;
  4. }else{
  5.     if(blabla)
  6.  
arrnum is a type int array and i am trying to store store the value from arrstr to it if the value in arrstr[i] is an integer
i don't have a very good understanding of the parseInt method does so i don't think this is right but i have no idea what to do.

i think i am way off and am still in the middle of coding this stuff but if you get what i am trying to do and can help that would be great.
i read the documentation for parseInt on
http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
but was kinda confused

thanks

eric
tolkienarda's Avatar
Needs Regular Fix
 
Join Date: Dec 2006
Posts: 316
#12: Sep 13 '07

re: Scanner class confusion


what i think i need to do is check to see if the NumberFormatException exception is thrown but i don't know how to do that

eric
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#13: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by tolkienarda

what i think i need to do is check to see if the NumberFormatException exception is thrown but i don't know how to do that

eric

You may need to go through a tutorial on Exception handling in Java first.
tolkienarda's Avatar
Needs Regular Fix
 
Join Date: Dec 2006
Posts: 316
#14: Sep 13 '07

re: Scanner class confusion


ok thanks

eric
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#15: Sep 13 '07

re: Scanner class confusion


Quote:

Originally Posted by tolkienarda

ok thanks

eric

If you want to, you can be one of the first to have a look at my (so far unfinished) Article about Exceptions in the Editors Corner. Here's the link: http://www.thescripts.com/forum/thread700155.html

Also, if something is unclear or missing, please say so, as then I can use this information for improvement.

Greetings,
Nepomuk
Reply