2 Replies - 880 Views - Last Post: 24 February 2009 - 05:18 PM Rate Topic: -----

#1 xJames720x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-February 09

New to Java. Need help with for-loop.

Posted 24 February 2009 - 04:07 PM

I began a Java class at my high school but the pace was going very slow so I downloaded a .pdf file of the book and began working ahead. Im having trouble with an exercise involving loops. I need to use a "for-loop" and the "substring" method to reverse the print of a string. Example: Jon Doe = eod noj. I am completely at a loss as of how to do this.

Here is what I have so far:

 

import java.io.*;
import java.util.*;

public class nameReversal
{
	public static void main(String args[])
	{
		Scanner kbReader = new Scanner(System.in);

		
		System.out.print("Please enter your name: ");
		String name = kbReader.next();
		
		for(
		{
			
		}
		
		
	}
}




I am confused as to how I use the for-loop and substring method to do this. If someone could point me in the right direction I would appreciate it! :)

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java. Need help with for-loop.

#2 tanah.melayu  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 21-February 09

Re: New to Java. Need help with for-loop.

Posted 24 February 2009 - 04:40 PM

here it is :) easy to use if you know and understand the idea :)

import java.util.*;
import java.io.*;

public class reverse_technique
{
  static Scanner console = new Scanner(System.in);
  
  private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  
  public static void main(String[] args)
	throws IOException
  {
	String x;
	
	System.out.println("Enter your name");
	x = stdin.readLine();
	
	System.out.print("Your name in backward: ");
	
	for(int i = (x.length())-1; i >= 0; i--)
	{
	  char c = x.charAt(i);
	  System.out.print(c);
	}
	
  }
}


~Happy Coding~
Was This Post Helpful? 1
  • +
  • -

#3 xJames720x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-February 09

Re: New to Java. Need help with for-loop.

Posted 24 February 2009 - 05:18 PM

Yay its working thanks for the help. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1