5 Replies - 696 Views - Last Post: 24 May 2012 - 06:45 PM Rate Topic: -----

#1 thienvina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-May 12

Need Help with JAVA String!

Posted 24 May 2012 - 12:22 AM

Need Help with JAVA String
Im trying to make it so that I can't to out put as the example! is only:
S: - - - - ; A: - A - A ; V: - - V -; J : J - - -
Help Please ME!

Thank you very much!
- - - -
S
- - - -
A
- A - A
V
- A V A
D
- A V A
J
J A V A
Bingo! You won.

This is my program:

import java.util.*;
       class GuessWordJava {
         public static void main ( String[] args ){
           Scanner scanner = new Scanner(System.in);
           System.out.print("\n Enter secret entence : ");
           String sentence = scanner.next();
                int length = sentence.length();
          while( true) {
            char letter;
            System.out.print("\n Enter your guess : ");
            char ch = scanner.next().charAt(0);
            if ( ch == ' ') {break;}
               String str="";
            for (int i = 0; i < length; i++){
               letter = sentence.charAt(i);
                 if ( letter == ch ){
                      str=str+letter;
                } else {
                      str= str+ "-";
                } 
              }
           System.out.print(str);
   }
  }
}

This post has been edited by jon.kiparsky: 24 May 2012 - 09:09 AM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with JAVA String!

#2 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 273
  • Joined: 02-February 09

Re: Need Help with JAVA String!

Posted 24 May 2012 - 03:59 AM

I really dont know what you are asking. You want to format your console output in a certain way? Also, use code tags for your code, its not hard.
Was This Post Helpful? 0
  • +
  • -

#3 thienvina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-May 12

Re: Need Help with JAVA String!

Posted 24 May 2012 - 04:12 AM

I want to output as :
- - - -
S
- - - -
A
- A - A
V
- A V A
D
- A V A
J
J A V A

Thank for you help!
Was This Post Helpful? 0
  • +
  • -

#4 eikkaj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 119
  • Joined: 20-December 08

Re: Need Help with JAVA String!

Posted 24 May 2012 - 09:08 AM

Put
 
around your code.

It seems like you're trying to do a sort-of hangman type game, right? Maybe you could store the sentence's letters into an array or something and copy them to a new one as they are being guessed. Once that array is full, complete.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Need Help with JAVA String!

Posted 24 May 2012 - 09:13 AM

I too am confused. What exactly are you asking for help with?

There are plenty of things you could do to improve this but rather than just throwing ideas at you, it would be nice to know what you want to improve.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Need Help with JAVA String!

Posted 24 May 2012 - 06:45 PM

View Postthienvina, on 24 May 2012 - 07:12 AM, said:

I want to output as :
- - - -
S
- - - -
A
- A - A
V
- A V A
D
- A V A
J
J A V A

Thank for you help!

Hangman I mean

Program outputs:
_ _ _ _

User enter "S"

Program outputs:
_ _ _ _

User enters "A"

Program outputs:
_ A _ A


You will have to learn how to descrive your problem, if you can't explain better a simple hangman what will it be when you will have to write a mortgage calculator using a GUI :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1