Jump to content

Printing out sentences with different names

- - - - -

  • Please log in to reply
4 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
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");

       

    }

}



#2
common_man

common_man

    Newbie

  • Members
  • Pip
  • 8 posts
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?

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

common_man said:

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.

Quote

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 :


#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
 

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.

#5
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Sinipull said:


 


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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users