deepdivin247's Profile User Rating: -----

Reputation: 1 Apprentice
Group:
Members
Active Posts:
22 (0.03 per day)
Joined:
20-April 12
Profile Views:
360
Last Active:
User is offline Sep 07 2012 07:43 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   deepdivin247 has not set their status

Posts I've Made

  1. In Topic: How to print out the lexicographically smallest word

    Posted 7 Sep 2012

    Hey, thanks for all your help.
    I ended up just turning in the assignment. Its due at 1159 tonight. I still want to figure it out but will do on my own time. Thank you so much for your help.
  2. In Topic: How to print out the lexicographically smallest word

    Posted 7 Sep 2012

    View Postblackcompe, on 07 September 2012 - 05:28 PM, said:

    Quote

    Now, while this does work and i'm okay with leaving it as it is, but I dont think this is exactly what the assignment is asking. Because i do need that Scanner.nextInt so that it can read the user input.


    My last post is probably the way your teacher intended it to be written and it does make use of Scanner.nextInt, so I'd go with that.

    Quote

    On the other hand, and correct me if i'm wrong, but my program is scanning that Int and putting it into my array [0]


    Yes, that's what's happening. It's ignoring that integer. Depending on how you write the code, that integer may or may not be necessary.



    So right now I have

    import java.util.Scanner;
    
    public class delete {
    
    	public static void main(String[] args) {
    		
    		Scanner sc6 = new Scanner(System.in);
    		int n = sc6.nextInt();
    		String str1 = sc6.nextLine();
    		for (int i=0; i<n; i++){
    			
    			String str2 = sc6.nextLine();
    		}
    			System.out.println(str1);
    		
    		sc6.close();
    	}
    }
    
    


    Which i'm sure you can tell or figure out, it's taking the input number lets say 4 and looping through those 4 times. I guess i dont understand exactly what to do. then i have it set to print out what it received from the loop, just to have the visual.

    With what you've said, I dont see how to compare the words to find the smallest one in the loop? i'm assuming java does that for us. But then how does it store those words and where?
  3. In Topic: How to print out the lexicographically smallest word

    Posted 7 Sep 2012

    View Postblackcompe, on 07 September 2012 - 05:03 PM, said:

    deepdivin247: You've got the code written already...

    • Get the input with a Scanner.
    • Split it into an array. Lose the first element (since it's an integer).
    • Sort the array and print the first element.


    Lose the for loop in your code and lose the Scanner.nextInt. You don't need it. You don't even need that integer. There are many ways to do this. I'm giving you one.


    Funny thing is, I was looking at Eclipse and going through my program testing it without certain stuff and this is what I came up with, before i read what you wrote.

    import java.util.Scanner;
    import java.util.Arrays;
    
    
    public class delete {
    
    	public static void main(String[] args) {
    		
    		Scanner sc6 = new Scanner(System.in);
    			
    			String str1 = sc6.nextLine();
    			String[] words = str1.split(" ");
    			Arrays.sort(words);
    			System.out.println(words[1]);
    	        System.out.println(Arrays.toString(words));
    		
    		sc6.close();
    	}
    }
    
    


    Now, while this does work and i'm okay with leaving it as it is, but I dont think this is exactly what the assignment is asking. Because i do need that Scanner.nextInt so that it can read the user input.

    On the other hand, and correct me if i'm wrong, but my program is scanning that Int and putting it into my array [0], and then my program goes through each word, sorts it, then prints out whatever word is in array[1]. Is that what's happening?

    Isn't my code just ignoring the integer I put in the front?

    View Postblackcompe, on 07 September 2012 - 05:15 PM, said:

    You can also do this without using arrays.

    • Get the number of words with Scanner.nextInt.
    • In a loop, get each word with Scanner.next.
    • If the word is smaller than the smallest word, it becomes the smallest. Since the smallest word will initially be null, consider reading a single word before entering the loop.


    I'm going to try and write the code out this way on the side to see if i can figure it out. It sounds like the correct way to go if i actually stay on the guidelines of the assignment.
  4. In Topic: How to print out the lexicographically smallest word

    Posted 7 Sep 2012

    View Postblackcompe, on 07 September 2012 - 04:49 PM, said:

    Quote

    Does sorting the words in the array put them in lexicographical order? And then from there I can print out the first word?


    Yes. With that being said, how you want to construct the array is up to you. Generally, if you don't know the size of the input, you'd use a dynamic structure (e.g. List). You can sort it with Collections.sort or convert it to an array with List.toArray.

    I can't give you concrete advice since I don't know what the constraints are. For instance, assuming this is an assignment, you may not be able to use dynamic data structures. If you must use an array, you can (1) assume an input size or (2) ask the user how many strings they'd like to enter. Another thing: Do you expect the user to enter one word per line? Those are things I'd rather not get involved with.


    Haha, no not one word per line. An example would be,

    Input:
    4 wait what is this 
    


    Output:
     is 
    


    I cant see the output on my laptop, but in that output code box, it should have the word "is".

    I'm actually not supposed to be using arrays since we technically havent learned them yet, but since ive taken a class in C, i sort of have an idea. So i can use them. Not sure what you mean by dynamic data structures so i'm gonna go with a no on using those. and the 4 in front of the words in my input above is where the user tells the program how many strings they'd like to enter....followed with the four words.
  5. In Topic: How to print out the lexicographically smallest word

    Posted 7 Sep 2012

    View Postblackcompe, on 07 September 2012 - 04:00 PM, said:

    deepdivin247: Strings are Comparable, which means calling String.compareTo(String), will return the (lexicographically) smaller of the two. Using that method as a foundation, Arrays.sort, will sort any Comparable set of objects.


    Okay I understand that. What i don't know how to do or what i just cant visualize is that for my program, i don't know how many strings the user will input each time. if it was a constant 4 or 5, i would be able to write the program to compare [0] with [3] and [1] with [4]. Am i saying that correctly? Do i use some variable "i" in place of strings and loop through to compare each? An int cannot be used to compare strings though....right?

    Does sorting the words in the array put them in lexicographical order? And then from there I can print out the first word?

    Scanner sc6 = new Scanner(System.in);
    		int n = sc6.nextInt();
    		for (int i=0; i<n; i++){
    			
    			String str1 = sc6.nextLine();
    			String[] words = str1.split(" ");
    			Arrays.sort(words);
    	        System.out.println(Arrays.toString(words));
    	        
    	        //System.out.println(words [0]);
    	        
    			for (int j = 1; j<words.length; j++){
    				//System.out.print(str1.length());
    			}
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

Comments

Page 1 of 1
  1. Photo

    David W Icon

    07 Jun 2012 - 01:45
    Nice problem you posted ... keep it up :)
Page 1 of 1