Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Printing out sentences with different names

java printing

  • Please log in to reply
4 replies to this topic

#1 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 16 September 2011 - 06:36 PM

I'm going to implement this or should I say place this code into my previous Jpanel program. Therefore it's basically a prototype I'm having trouble with.

Here is where the problem is
System.out.println("First you saw, "+name1);
                            System.out.println("You then met , "+name2);
                                System.out.println("But, "+name3+" got in the way");
                                    System.out.println("Though, "+name4+" helped you to recover your conversation");
                                        System.out.println("Finally, "+ name3 +" apologized because "+name5 +" explained the situation");

Problem, is asking me to input a value into a variable when I want to print the preview input value

Here is the code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

 import java.util.Scanner;
/**
 *
 * @author SOAD
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int i = 1;
        Scanner input = new Scanner(System.in);
        String name1,name2,name3,name4,name5;
        
        
        
    while(i<5)
        {
         System.out.println("Please enter 5 names");
          System.out.print("Name 1 :"); System.out.println(name1= input.nextLine());
            System.out.print("Name 2 :"); System.out.println(name2=input.nextLine());
                System.out.print("Name 3 :"); System.out.println(name3=input.nextLine());
                    System.out.print("Name 4 :"); System.out.println(name4=input.nextLine());
                        System.out.print("Name 5 :"); System.out.println(name5=input.nextLine());
          
                i++;
                       }
    
                        System.out.println("First you saw, "+name1);
                            System.out.println("You then met , "+name2);
                                System.out.println("But, "+name3+" got in the way");
                                    System.out.println("Though, "+name4+" helped you to recover your conversation");
                                        System.out.println("Finally, "+ name3 +" apologized because "+name5 +" explained the situation");
       
    }
}

  • 0

#2 common_man

common_man

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 16 September 2011 - 11:42 PM

Can you tell what output are you getting? When I run the program, I have to input each name 5 times (because of the While loop) and then the output is displayed taking the last entered set of names.
As far as the code goes, the program is running according to the code. Were you expecting any other ouput?
  • 0

#3 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 17 September 2011 - 08:01 AM

Can you tell what output are you getting? When I run the program, I have to input each name 5 times (because of the While loop) and then the output is displayed taking the last entered set of names.
As far as the code goes, the program is running according to the code. Were you expecting any other ouput?


Well I'm trying to get the names to be displayed after the user has entered them, but it seems to me they are still holding the input value and I want them to actually output the entered value and that the variable has not been initialized.
I need to do this for my Java class with 10 variables a while loop and a for loop and inside of a Jpanel so therefore I'm trying to go in with that perspective.

run:
Please enter 5 names
Name 1 :Me
Me
Name 2 :you
you
Name 3 :he
he
Name 4 :she
she
Name 5 :they
they
Please enter 5 names
Name 1 :sdf
sdf
Name 2 :hjk
hjk
Name 3 :ghj
ghj
Name 4 :hjk
hjk
Name 5 :hjk
hjk
Please enter 5 names
Name 1 :


  • 0

#4 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 18 September 2011 - 03:35 AM

 

while(i<5)         {          System.out.println("Please enter 5 names");           System.out.print("Name 1 :"); System.out.println(name1= input.nextLine());             System.out.print("Name 2 :"); System.out.println(name2=input.nextLine());                 System.out.print("Name 3 :"); System.out.println(name3=input.nextLine());                     System.out.print("Name 4 :"); System.out.println(name4=input.nextLine());                         System.out.print("Name 5 :"); System.out.println(name5=input.nextLine());

This part of code iterates 5 times, which means you will be asked for 25 names.


String name1,name2,name3,name4,name5;
Instead of this try to use
String[] names = new String[5];

and then use
for(int i = 0; i < names.length(); i++){
    System.out.print("Name "+(i+1)+" :"); 
    names[i] = input.nextLine());
}

to read the names.
  • 0

#5 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 18 September 2011 - 05:56 AM

 

while(i<5)         {          System.out.println("Please enter 5 names");           System.out.print("Name 1 :"); System.out.println(name1= input.nextLine());             System.out.print("Name 2 :"); System.out.println(name2=input.nextLine());                 System.out.print("Name 3 :"); System.out.println(name3=input.nextLine());                     System.out.print("Name 4 :"); System.out.println(name4=input.nextLine());                         System.out.print("Name 5 :"); System.out.println(name5=input.nextLine());

This part of code iterates 5 times, which means you will be asked for 25 names.


String name1,name2,name3,name4,name5;
Instead of this try to use
String[] names = new String[5];

and then use
for(int i = 0; i < names.length(); i++){
    System.out.print("Name "+(i+1)+" :"); 
    names[i] = input.nextLine());
}

to read the names.


Thanks got it.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download