473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Scanner class confusion

tolkienarda
316 100+
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
Sep 13 '07 #1
14 2380
madhoriya22
252 100+
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.  
Sep 13 '07 #2
Nepomuk
3,112 Expert 2GB
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
Sep 13 '07 #3
madhoriya22
252 100+
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 :)
Sep 13 '07 #4
Nepomuk
3,112 Expert 2GB
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
Sep 13 '07 #5
madhoriya22
252 100+
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 :)
Sep 13 '07 #6
Nepomuk
3,112 Expert 2GB
Hi,
Again nice description buddy :)
And again, I just do my best! ^^
Sep 13 '07 #7
tolkienarda
316 100+
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
Sep 13 '07 #8
r035198x
13,262 8TB
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?
Sep 13 '07 #9
r035198x
13,262 8TB
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.
Sep 13 '07 #10
tolkienarda
316 100+
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
Sep 13 '07 #11
tolkienarda
316 100+
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
Sep 13 '07 #12
r035198x
13,262 8TB
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.
Sep 13 '07 #13
tolkienarda
316 100+
ok thanks

eric
Sep 13 '07 #14
Nepomuk
3,112 Expert 2GB
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
Sep 13 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Dan =o\) | last post by:
Hey guys, I wonder if you could please provide me with some ideas as to how to get around this problem. Symbol MC9000-k, Pocket PC 2003... With a scanner application I've written, data is...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
1
madhoriya22
by: madhoriya22 | last post by:
Hi, My JVM does not support java.util.Scanner class. Is it too new or too old for Scanner class. Currently I am working on jdk1.5.0_04. While using Scanner class java compiler shows this message :-...
3
by: thename1000 | last post by:
Hi, I'm trying to create this output: Input team 1's name: Team 1 Input team 1's ranking: 90.4 etc.
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
7
by: kidosai | last post by:
hi i need a guide on how to use the scanner class... i need to use Java Scanner class to read a text file and be able to print the text inside of it... example : i have a text file named...
2
by: bvav22 | last post by:
Hey guys, im trying to finish an assignment for a java class, and i have finished the program except for one part. The program requires that i take 3 values inputted from the user, the first 2 must...
6
by: rotaryfreak | last post by:
Hi everyone, ive had this problem for a while and i cant seem to figure out why. I am using eclipse to create my java code. When import the Scanner class, create a new object and so on... ...
1
by: Aycex | last post by:
I am taking my first java class as i am a Database person this is quite interesting and here is my problem. We are supposed to be learning to overload a class and i believe i have my code correct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.