Nested for loop?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 946 Views - Last Post: 11 March 2016 - 04:16 PM Rate Topic: -----

#1 rahulchawla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-March 16

Nested for loop?

Posted 10 March 2016 - 12:30 PM

I'm trying to write a small program to print a line as the program evaluates it regardless of spaces example-

input "hello my name is choppa down"

output-
hello
my
name
is
choppa
down

I know what i need to do i need to
use string and CharAt- use loop that goes i=0 to end of entire line-nested loop until it finds beginning of a word non space remember it, one more loop going till it finds a space end of line remember it

but im having problem writing a nested for loop what i have so far is can someone help please?

import java.util.Scanner;
public class words1 {
  public static void main (String [] args){
    
    Scanner sc = new Scanner(System.in);
    String line;
    System.out.println("Please enter a line of text");
    line= sc.nextLine();
    
    for (i=0, i<line.length, i++){
     
  }
}
    
  


Is This A Good Question/Topic? 0
  • +

Replies To: Nested for loop?

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Nested for loop?

Posted 10 March 2016 - 12:38 PM

The loop defined on line 10 iterates one time for each character in the line variable.
What is to be done with the value of i inside the outer loop?
What is supposed to be done in the inner loop? Why is it needed?
Was This Post Helpful? 0
  • +
  • -

#3 rahulchawla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-March 16

Re: Nested for loop?

Posted 10 March 2016 - 12:44 PM

Im not sure tbh, thats what I assumed you would do to loop through the line of text?
Was This Post Helpful? 0
  • +
  • -

#4 ClothTiger   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 05-March 16

Re: Nested for loop?

Posted 10 March 2016 - 12:45 PM

Do you want to split a sentence into words and print them one per line?
Was This Post Helpful? 1
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Nested for loop?

Posted 10 March 2016 - 12:47 PM

Quote

loop through the line of text?

Yes that's what the loop at line 10 would do.
What would another loop do?
Was This Post Helpful? 0
  • +
  • -

#6 rahulchawla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-March 16

Re: Nested for loop?

Posted 10 March 2016 - 12:51 PM

I need to do another loop to find a start of a word (nonspace)
and cloth tiger yes thats what i mean

Im having problem with writing the theory behind the nested loop
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Nested for loop?

Posted 10 March 2016 - 01:02 PM

Quote

I need to do another loop to find a start of a word (nonspace)

Why can't that be done in one loop where the code looks at each letter in turn and decides what to do with it depending on what state the code in the loop is in: looking for start of word or looking for end of word.
Like what you said here:

Quote

until it finds beginning of a word non space remember it,
... going till it finds a space end
\
That is all done in one loop.

This post has been edited by NormR: 10 March 2016 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 ClothTiger   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 05-March 16

Re: Nested for loop?

Posted 10 March 2016 - 01:23 PM

Forget about the nested loop, it's not needed.
You want to split a String. I'm pretty sure you can now figure out what the method is called and in which class it's found. ;)

String s = "Foo Bar Fighters Play Ball";
// split up the string into (5; or how many words the sentence happens to have) words here
for (each word in s) {
  println(word)
}


output:

Foo
Bar
Fighters
Play
Ball

Link to String class: https://docs.oracle....ang/String.html

This post has been edited by ClothTiger: 10 March 2016 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Nested for loop?

Posted 10 March 2016 - 01:27 PM

Is this an exercise on how to use loops and indexes to separate words
or is it on how to use java SE class methods to do it?
Was This Post Helpful? 0
  • +
  • -

#10 rahulchawla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-March 16

Re: Nested for loop?

Posted 10 March 2016 - 03:17 PM

Yes but i would require input from the user in which case the string is not specified and could have many spaces
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Nested for loop?

Posted 10 March 2016 - 03:20 PM

Can you use Java SE classes and methods to split the String and print the results?
Was This Post Helpful? 0
  • +
  • -

#12 ClothTiger   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 05-March 16

Re: Nested for loop?

Posted 10 March 2016 - 03:59 PM

View Postrahulchawla, on 10 March 2016 - 11:17 PM, said:

Yes but i would require input from the user in which case the string is not specified and could have many spaces

Just replace my predefined string with whatever the user inputs. I've given you the tools to solve this (pseudo code and direct link). Give it a go and post your results.
Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Nested for loop?

Posted 10 March 2016 - 04:18 PM

The first line of your main method creates a class that already knows where the boundaries between words are
Was This Post Helpful? 0
  • +
  • -

#14 rahulchawla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-March 16

Re: Nested for loop?

Posted 10 March 2016 - 08:37 PM

I got it guys but i want it to print the words without spaces in between them like right under each other here is my current code
  import java.util.Scanner;
public class words1 {
  public static void main (String [] args){
    
    Scanner sc = new Scanner(System.in);
   
    System.out.println("Please enter a line of text");
   String line= sc.nextLine();
    
   String [] b= line.split(" ");
   
    for(int i=0;i<b.length;i++){
      System.out.println(b[i]); }
   
}
  }


    
  
    

Was This Post Helpful? 0
  • +
  • -

#15 ClothTiger   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 05-March 16

Re: Nested for loop?

Posted 10 March 2016 - 10:25 PM

Well..you already know how to 1) ask for input 2) make a for/while loop. Which means you know how to begin.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2