Results 1 to 12 of 12
  1. #1
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Writing a program with String Scanner

    Hi

    I found about a thousand new photos that i'm going to scan into my computer, and in the future I want to be able to search for a person that is in some of the pictures, so I decided to write a program that when I write the guy's name in it shows me what pictures he's in. I know that I wan't to use a scanner method but I can't figure out how what method I can use to write a String in the scanner that doesn't just return what I wrote in the scanner, for example I want to be able to write Derp and it shows "Derp is in pictures number 1, 3, 7 etc"

    If anyone knows what method I can use I'd really appreciate it.

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

    Default Re: Writing a program with String Scanner

    I am not certain what you mean by scanner method. However, if I were going to do this I would simply construct a simple data base which maps names to image contents. Then you just query the data base for the desired names and retrieve the associated images.

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

  3. #3
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Writing a program with String Scanner

    What I mean by the scanner method is that I am using eclipse and if I want to write something into the program instead of the program just giving me an answer I have to use a scanner, also that was exactly what I was going to program but I'm not really sure how, I've tried a few methods but none of them are working for me, by method I mean like if and else statements etc.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,417
    Rep Power
    25

    Default Re: Writing a program with String Scanner

    I have to use a scanner
    Are you talking about hardware that "scans" a piece of paper(a photo) and creates a computer disk image file?

    Given a number of images that were scanned into the computer, what do you want to do with them?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Writing a program with String Scanner

    I am talking about a method in eclipse that you can use to write something in the program, for example when I programmed a simple calculator I used this code :
    package day1.Starting;

    import java.util.Scanner;

    public class CalculatorYoutube {
    public static void main(String[] args) {

    Scanner inputFromUser = new Scanner(System.in);
    double fnum, snum, answer;
    System.out.println("Enter First Number");
    fnum = inputFromUser.nextDouble();
    System.out.println("Enter Second Number");
    snum = inputFromUser.nextDouble();
    answer = fnum + snum;
    System.out.println("Answer is " + answer);
    }

    }

    and what I want to do is for example if I write a name in the program that I have already established as "1, 2, 3, 7" etc I want the program to say "name" is on pictures number 1, 2, 3, 7

    I can't use the if and else method because I can't use a boolean method, I have to use a String method, I tried this method :
    import java.util.Scanner;



    public class StartingPoint {

    public static void main(String[] args) {


    String testi;
    Scanner scan = new Scanner(System.in);

    System.out.println("Testing Scanner, write something: ");
    testi = scan.nextLine();
    System.out.println(testi);
    }




    }

    But that only returns what you write in, that's what I'm asking for, a method that I can use so that I don't get what I wrote back but if I write Derp in i get Derp is in pictures nr. 1, 2, 3, 7 or something like that.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,417
    Rep Power
    25

    Default Re: Writing a program with String Scanner

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    The Scanner class is in the Java SE classes. The Scanner class has methods that can be used to read input from a user entering it via console accessed via System.in.

    i get Derp is in pictures nr. 1, 2, 3, 7
    How will the program connect the String: "Derp" with those pictures?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Writing a program with String Scanner

    Well I tried connecting it like this:

    Java Code:
    String Derp = "1, 2, 3, 7";
    and then printing out Derp("1, 2, 3, 7") if that is written in but I can't figure out how to do that

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,417
    Rep Power
    25

    Default Re: Writing a program with String Scanner

    The normal way to associate one String with another String is to use a Map<String, String>. The first String is the name and the second String is the numbers. I assume this technique is past your current knowledge.

    Another way would be a switch statement with the name as the selector variable and then have case statements for each known name that returns the String of numbers.

    I have to use a String method
    If that is the requirement, I have an idea but it is unusual and awkward. Create a long String with all the known names followed by the numbers with some delimiters separating them: name1;numbers for name1;name2;numbers for name2; etc
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Writing a program with String Scanner

    Well I tried it with a switch statement but I get an error, not sure if i'm doing it right but this is how I did it :

    Java Code:
    public class StartingPoint {
    
    	public static void main(String[] args) {
    	
    		Scanner inputFromUser = new Scanner(System.in);
    		
    		String name;
    		name = inputFromUser.next();
    		
    		
    		switch (name){
    		
    		case Derp:
    			System.out.println("1, 2, 3, 7");
    			break;
    		
    		}
    		
    		}
    		
    		
    		
    	
    }

  10. #10
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Writing a program with String Scanner

    Never mind I figured it out, just forgot the " before and after Derp :P

  11. #11
    Skaeringur97 is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Writing a program with String Scanner

    Thanks a lot Norm, I really couldn't have figure it out without you.

    Now I know exactly how I'm going to finish this program :D

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,417
    Rep Power
    25

    Default Re: Writing a program with String Scanner

    Glad you got it working.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: 07-02-2013, 01:59 AM
  2. Replies: 1
    Last Post: 11-11-2012, 01:55 AM
  3. how to use Scanner to scan a String
    By RichersooN in forum New To Java
    Replies: 1
    Last Post: 09-23-2011, 05:14 AM
  4. String tokenizing with Scanner
    By vijaygk in forum Advanced Java
    Replies: 2
    Last Post: 07-15-2008, 04:44 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 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
  •