Scanner class confusion  | Needs Regular Fix | | Join Date: Dec 2006
Posts: 316
| |
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 -
import java.util.*;
-
public class asg3 {
-
static Scanner console = new Scanner(System.in);
-
public static void main(String[] args) {
-
String[] arrnums = new String[20];
-
String input = " ";
-
Scanner parse = new Scanner(input).useDelimiter("\\s*");
-
int i = 0;
-
Random ran = new Random();
-
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");
-
input = console.nextLine();
-
System.out.print(input);
-
while(arrnums[i] = parse.next())
-
{
-
System.out.println(i);
-
//and some other stuff
-
i++;
-
}
-
-
}
-
}
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
|  | Familiar Sight | | Join Date: Jul 2007 Location: India
Posts: 254
| | | 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 -
import java.util.*;
-
public class asg3 {
-
static Scanner console = new Scanner(System.in);
-
public static void main(String[] args) {
-
String[] arrnums = new String[20];
-
String input = " ";
-
Scanner parse = new Scanner(input).useDelimiter("\\s*");
-
int i = 0;
-
Random ran = new Random();
-
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");
-
input = console.nextLine();
-
System.out.print(input);
-
while(arrnums[i] = parse.next())
-
{
-
System.out.println(i);
-
//and some other stuff
-
i++;
-
}
-
-
}
-
}
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 -
-
while(arrnums[i] = parse.next())
-
is not correct. You should compare them using equals() method of string like this .. -
-
while(arrnums[i].equals(parse.next()))
-
|  | Moderator | | Join Date: Aug 2007 Location: Germany
Posts: 2,466
| | | re: Scanner class confusion Quote:
Originally Posted by madhoriya22 The way you are comparing two strings -
-
while(arrnums[i] = parse.next())
-
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
|  | Familiar Sight | | Join Date: Jul 2007 Location: India
Posts: 254
| | | 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 :)
|  | Moderator | | Join Date: Aug 2007 Location: Germany
Posts: 2,466
| | | 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 -
String str1 = "Hello";
-
String str2 = "Hell";
-
str2 += "o";
-
System.out.println(str1 == str2);
-
the output will be "false", although both Strings have the value "Hello", as the Objects addresses are checked, instead of their values. -
String str1 = "Hello";
-
String str2 = str1;
-
System.out.println(str1 == str2);
-
will return "true".
Greetings,
Nepomuk
|  | Familiar Sight | | Join Date: Jul 2007 Location: India
Posts: 254
| | | 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 -
String str1 = "Hello";
-
String str2 = "Hell";
-
str2 += "o";
-
System.out.println(str1 == str2);
-
the output will be "false", although both Strings have the value "Hello", as the Objects addresses are checked, instead of their values. -
String str1 = "Hello";
-
String str2 = str1;
-
System.out.println(str1 == str2);
-
will return "true".
Greetings,
Nepomuk Hi,
Again nice description buddy :)
|  | Moderator | | Join Date: Aug 2007 Location: Germany
Posts: 2,466
| | | re: Scanner class confusion Quote:
Originally Posted by madhoriya22 Hi,
Again nice description buddy :) And again, I just do my best! ^^
|  | Needs Regular Fix | | Join Date: Dec 2006
Posts: 316
| | | 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
| | | 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
| | | 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.
|  | Needs Regular Fix | | Join Date: Dec 2006
Posts: 316
| | | 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 -
if(arrnum[j].parseInt(arrstr[i]))
-
{
-
j++;
-
}else{
-
if(blabla)
-
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
|  | Needs Regular Fix | | Join Date: Dec 2006
Posts: 316
| | | 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
| | | 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.
|  | Needs Regular Fix | | Join Date: Dec 2006
Posts: 316
| | | re: Scanner class confusion
ok thanks
eric
|  | Moderator | | Join Date: Aug 2007 Location: Germany
Posts: 2,466
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|