Results 1 to 12 of 12
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Question Reading Multiple Numbers from a String

    I'm struggling with making what the title says work. This is what I've tried but unfortunately it doesn't work :( Can you guys help me?

    Java Code:
    import java.util.*;
    
    public class Ostbågar{
        public static void main(String[] args){
            String data = "17 70 186";
            Vector numbers = new Vector();
            Scanner hej = new Scanner(data);
            while(hej.hasNextInt()){
                numbers.add(hej.nextInt());
            }
        }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,511
    Rep Power
    5

    Default Re: Reading Multiple Numbers from a String

    You haven't explained what the problem is. But since you aren't doing any I/O, how do you know it doesn't work?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Re: Reading Multiple Numbers from a String

    Because the compiler (BlueJ) says that there is something wrong, and I've also tested it with output but it doesn't output anything. The problem is that I can't read the numbers from the string.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Reading Multiple Numbers from a String

    What does the compiler report? Does it compile? Do you get Exceptions? Do you have a stack trace?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,511
    Rep Power
    5

    Default Re: Reading Multiple Numbers from a String

    The only thing I see (I haven't tried it yet), is that you are using Vector as a raw type. But that should just issue a warning. I assume your file is the same name as your public class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Re: Reading Multiple Numbers from a String

    Quote Originally Posted by jim829 View Post
    The only thing I see (I haven't tried it yet), is that you are using Vector as a raw type. But that should just issue a warning. I assume your file is the same name as your public class.
    I tried "Vector<int>" but that was not allowed, and yes, an issue was reported. The file name is the same name as my public class? No, the file name is "hej.txt" and the public class name is Ostbågar, at least in the program above, but I did a new version of the program. (The program above did not describe its real purpose). What the program should really do is to read a text file containing several people's lengths, weights and ages, and then calculate their BMI (Body Mass Index) which is weight in kilograms divided by their length squared in meters (height/(length^2)). If a person's BMI exceeds 30, the program should output those people's names and data in another text file. But unfortunately I still get an error, not the same error though. (The error is on line 22)

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class hej{
    	public static void main(String[] args){
    		File file = new File("hej.txt");
    		try{
    			Scanner hej = new Scanner(file);
    			while(hej.hasNext()){
    				String s1 = hej.nextLine();
    				if(s1 == null)
    					break;
    				String s = hej.nextLine();
    				Vector numbers = new Vector(); // Vector is a raw type. References to generic type Vector<E> should be parameterized.
    				for(int x=0;x<s.length();x++){
    					int start = x;
    					while(s.charAt(x) != ' ' && x != s.length()-1)
    						x++;
    					int end = x;
    					numbers.add(Integer.parseInt(s.substring(start, end))); // Type safety: The method add(Object) belongs to the raw type Vector. References to generic type Vector<E> should be parameterized.
    				}
    				if(numbers.get(1) / (numbers.get(2)/100) > 30){      // ERROR: The operator / is undefined for the argument type(s) Object, int
    					PrintStream ps = new PrintStream(new FileOutputStream("hej1.txt", false));
    					ps.println(s1 + "\n" + numbers.get(0) + " " + numbers.get(1) + " " + numbers.get(2));
    				}
    			}
    		}catch(FileNotFoundException e){
    		}
    	}
    }

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,511
    Rep Power
    5

    Default Re: Reading Multiple Numbers from a String

    You need to show the actual error message generated by the compiler. Otherwise, we are just guessing. And generic type arguments must be classes or interfaces, not primitive types.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Re: Reading Multiple Numbers from a String

    I did show it, like I said before it is on line 22.
    ERROR: The operator / is undefined for the argument type(s) Object, int

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Reading Multiple Numbers from a String

    That means what it says: it doesn't make any sense to try to apply arithmetic division to an Object. An Object could be anything.
    Get in the habit of using standard Java naming conventions!

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,511
    Rep Power
    5

    Default Re: Reading Multiple Numbers from a String

    If you change Vector to a generic type of Vector<Integer> that particular error will go away.

    Regards,
    Jim
    Last edited by jim829; 10-15-2013 at 03:36 PM. Reason: Re-read the post
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Re: Reading Multiple Numbers from a String

    Oh. I tried changing the Vector to Vector<int> before because I thought that was how it should be written, but apparently I was wrong, thanks :)

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reading Multiple Numbers from a String

    A Vector stores objects; an int isn't such an object; try Integer for a change ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2012, 08:02 PM
  2. Replies: 9
    Last Post: 01-26-2012, 01:04 PM
  3. Reading numbers
    By edprog in forum New To Java
    Replies: 10
    Last Post: 04-16-2011, 07:17 AM
  4. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  5. Multiple Random Numbers
    By Jadles in forum New To Java
    Replies: 6
    Last Post: 02-26-2009, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •