Scanner class confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tolkienarda
    Contributor
    • Dec 2006
    • 316

    Scanner class confusion

    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
    Code:
     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
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    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
    Code:
    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
    Code:
     
    while(arrnums[i] = parse.next())
    is not correct. You should compare them using equals() method of string like this ..
    Code:
     
    while(arrnums[i].equals(parse.next()))

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by madhoriya22
      The way you are comparing two strings
      [CODE=java]
      while(arrnums[i] = parse.next())
      [/CODE]
      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

      Comment

      • madhoriya22
        Contributor
        • Jul 2007
        • 251

        #4
        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 :)

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          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
          [CODE=java]
          String str1 = "Hello";
          String str2 = "Hell";
          str2 += "o";
          System.out.prin tln(str1 == str2);
          [/CODE]the output will be "false", although both Strings have the value "Hello", as the Objects addresses are checked, instead of their values.
          [CODE=java]
          String str1 = "Hello";
          String str2 = str1;
          System.out.prin tln(str1 == str2);
          [/CODE]will return "true".

          Greetings,
          Nepomuk

          Comment

          • madhoriya22
            Contributor
            • Jul 2007
            • 251

            #6
            Originally posted by nepomuk
            In fact, it won't give you the result you want in most cases - if you define
            [CODE=java]
            String str1 = "Hello";
            String str2 = "Hell";
            str2 += "o";
            System.out.prin tln(str1 == str2);
            [/CODE]the output will be "false", although both Strings have the value "Hello", as the Objects addresses are checked, instead of their values.
            [CODE=java]
            String str1 = "Hello";
            String str2 = str1;
            System.out.prin tln(str1 == str2);
            [/CODE]will return "true".

            Greetings,
            Nepomuk
            Hi,
            Again nice description buddy :)

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by madhoriya22
              Hi,
              Again nice description buddy :)
              And again, I just do my best! ^^

              Comment

              • tolkienarda
                Contributor
                • Dec 2006
                • 316

                #8
                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

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  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?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    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.

                    Comment

                    • tolkienarda
                      Contributor
                      • Dec 2006
                      • 316

                      #11
                      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
                      Code:
                      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#pa rseInt(java.lan g.String)
                      but was kinda confused

                      thanks

                      eric

                      Comment

                      • tolkienarda
                        Contributor
                        • Dec 2006
                        • 316

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

                        eric

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by tolkienarda
                          what i think i need to do is check to see if the NumberFormatExc eption 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.

                          Comment

                          • tolkienarda
                            Contributor
                            • Dec 2006
                            • 316

                            #14
                            ok thanks

                            eric

                            Comment

                            • Nepomuk
                              Recognized Expert Specialist
                              • Aug 2007
                              • 3111

                              #15
                              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

                              Comment

                              Working...