14 Replies - 3106 Views - Last Post: 01 February 2011 - 09:06 AM Rate Topic: -----

#1 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Noob > need to make ascii triangle/pig latin program

Posted 29 January 2011 - 10:12 PM

Hi guys,
for my second program, I need to produce a triangle when the user inputs a number.
Here are some sample input and corresponding output:

Welcome to the equilateral triangle drawing program.
Enter the length of the side:
4
Here you go:
        * * * *
         * * *
          * *
           *



Here is different run:

Welcome to the equilateral triangle drawing program.
Enter the length of the side:
7
Here you go:
        * * * * * * *
         * * * * * *
          * * * * *
           * * * *
            * * *
             * *
              *


I have no idea where to begin/what methods to use. it's my 2nd program which I think is a big jump from my last one, which was to calculate euclidean distance.

also, for my third lab, I have to create a program that takes the input of a user and spits out the pig latin version. I'm already past the due date on that, but I still want to understand what my errors were.

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

class Pig{
   public static void main (String[] args)
   {
     System.out.print("Welcome to the Pig, please enter a");
     System.out.println("word/sentence/phrase.");
     Scanner scan = new Scanner(System.in);
     int x;
     String goagain, sentence, end, transfer;
     String vowel = "way"; //suffix for words beginning with vowel
     String consonant = "ay"; //suffix for words beginning with consonant

   do{
     while(scan.hasNext())
     {
     sentence = scan.next(); //input to be oinkified

       sentence = sentence.toLowerCase();
       for( x = 0; x < sentence.length(); x++)
       {
          if(sentence.charAt(0)=='a'||sentence.charAt(0)=='e'||
             sentence.charAt(0)=='i'||sentence.charAt(0)=='o'||
             sentence.charAt(0)=='u')
          {
             System.out.print(sentence+vowel+" ");
             break;
          }
          else if(sentence.charAt(x)=='a'||sentence.charAt(x)=='e'||
                  sentence.charAt(x)=='i'||sentence.charAt(x)=='o'||
                  sentence.charAt(x)=='u')
          {
          transfer = sentence.substring(0, x);
          end = sentence.substring(x, sentence.length());
          System.out.println(end+transfer+consonant+" ");

             break;

          }
       }
     }  
       System.out.println("Do you want to translate more?");   
       goagain = scan.next();
     } while(goagain == "yes");
     System.out.println("Goodbye!");
   }
}


writing programs like "hello world!" and area and distance calculators was fun but now it's getting really hard X_X, at least for me as I've got no previous experience.

thanks in advance

This post has been edited by macosxnerd101: 29 January 2011 - 11:43 PM
Reason for edit:: Added code tags around the triangles


Is This A Good Question/Topic? 0
  • +

Replies To: Noob > need to make ascii triangle/pig latin program

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Noob > need to make ascii triangle/pig latin program

Posted 29 January 2011 - 11:45 PM

For your first program, look into using nested for loops, as well as the System.out.print() and System.out.println() methods.

For your pig latin program, what problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 01:01 AM

for my second program, the pig latin-izing works but it's infinitely looped. to combat that problem, I tried to place the "while(scan.hasNext))" block into a do while block but i couldn't figure out how to end it or if it's even possible. to be honest I'm not even too sure what all the implications of scan.hasNext are.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 01:07 AM

Could this be because you are using hasNext() and next() instead of hasNextLine() and nextLine()? With the former, if a full sentence is inputted, you will get unexpected behaviors due to next() only reading up to the first instance of whitespace. nextLine() reads to the end of the line.
Was This Post Helpful? 0
  • +
  • -

#5 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 01:23 AM

I'm not exactly too sure about what you mean but I went and changed my scan.hasNext() to scan.hasNextLine() and my scan.next() to scan.nextLine() and the program is still infinitely looped. it never gets a chance to evaluate the System.out.println("do you want to continue");
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 01:27 AM

That's because hasNextLine() will loop as long as there is input being submitted. If you want it to prompt after every one, move the text inside of the while loop and get rid of the the do loop. If the user ever says yes that they want to quit, use the break keyword to break out of the loop.
Was This Post Helpful? 0
  • +
  • -

#7 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 01:52 AM

alright... so thanks to your tip, I was able to get the program to stop looping and exit after one input. I didn't realize I could add a break; after a block. i just learned switch case a few days ago which is when i was first introduced to breaks so i thought they could only be applied in those situations.

but now my problem is such: if I enter "hello my name is", my output is only ellohay. it ignores the "my name is" bit

so now I'm confused as to what my while loop was doing. was it the reason the program doesn't exit or does the function of scan.hasNext() just keep checking the sentence and applying the statement executions to each word in the sentence?
import java.util.*;

import java.io.*;

class Pig{
   public static void main (String[] args)
   {
     System.out.print("Welcome to the Pig, please enter a");
     System.out.println("word/sentence/phrase.");
     Scanner scan = new Scanner(System.in);
     int x;
     String goagain, sentence, end, transfer;
     String vowel = "way"; //suffix for words beginning with vowel
     String consonant = "ay"; //suffix for words beginning with consonant

     while(scan.hasNext())
     {
     sentence = scan.next(); //input to be oinkified

       sentence = sentence.toLowerCase();
       for(x = 0; x < sentence.length(); x++)
       {
          if(sentence.charAt(0)=='a'||sentence.charAt(0)=='e'||
             sentence.charAt(0)=='i'||sentence.charAt(0)=='o'||
             sentence.charAt(0)=='u')

             System.out.print(sentence+vowel+" ");


          else if(sentence.charAt(x)=='a'||sentence.charAt(x)=='e'||
                  sentence.charAt(x)=='i'||sentence.charAt(x)=='o'||
                  sentence.charAt(x)=='u')
          {
          transfer = sentence.substring(0, x);
          end = sentence.substring(x, sentence.length());
          System.out.println(end+transfer+consonant+" ");
             break;
          }
       }
  System.out.println("goodbye!");
   break;
     }
   }
}


This post has been edited by petadeer: 30 January 2011 - 01:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 01:54 AM

Again, that's due to the next(). next() only reads up to the first whitespace (in this case, the space). You want nextLine() which reads until the next enter. This also means that you will need hasNextLine() in your while loop as well.
Was This Post Helpful? 0
  • +
  • -

#9 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 02:10 AM

Thanks for the quick responses and being patient... I still get another error but I will try and figure it out...

I have changed the hasNext() to hasNextLine() and the scan.next() to scan.nextLine() but now when I input "hello my name is" the output is "ello my name ishay". so it shows the rest of the sentence now but does not pig latinize each word.
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 02:12 AM

Yes, you'll have to split each part of the sentence into its own word, add on the change and then stick them back together again. you can easily split based on whitespace using the string.split() method().
Was This Post Helpful? 0
  • +
  • -

#11 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 06:43 AM

NICE... just an update: I figured out how to do the ascii triangle program. nested for loops was kind of confusing but my book thoroughly explains them. I'm stumped with the Pig Latin one because there are so many new methods that I don't quite understand precisely enough.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Noob > need to make ascii triangle/pig latin program

Posted 30 January 2011 - 09:52 AM

The Java documentation is a great place to start for the methods:
Scanner class
String class
Was This Post Helpful? 0
  • +
  • -

#13 petadeer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Noob > need to make ascii triangle/pig latin program

Posted 01 February 2011 - 02:22 AM

I'm reading about hasNext and the description is such: Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

what does 'the scanner does not advance past any input' mean? does that mean once i input something, this loops forever?

i just learned about creating methods and referring to them. would the solution to my pig latin program to make that hasNext bit a method and refer to it?

also I looked into the scanner link you sent and I don't understand the bit in the beginning

     String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
     System.out.println(s.nextInt());
     System.out.println(s.nextInt());
     System.out.println(s.next());
     System.out.println(s.next());
     s.close(); 



and

     String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input);
     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
     MatchResult result = s.match();
     for (int i=1; i<=result.groupCount(); i++)
         System.out.println(result.group(i));
     s.close(); 



what is that //s+ and //d+ and //w+ stuff?

and I've googled it and still not exactly sure what tokenizing means...
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Christine*


Reputation:

Re: Noob > need to make ascii triangle/pig latin program

Posted 01 February 2011 - 08:28 AM

class star_7
    {
        public static void main(int n)
        {
            int i,k,j;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=(i-1);j++)
                {
                    System.out.print(" ");
                }
                for(k=1;k<=(2*(n-i)+1);k++)
                {
                    System.out.print("*");
                }
                        System.out.println(" ");
                    }
                }
            }
                    


this is the easiest solution to the program of the Star pattern . Java is fun. Hope you enjoy programming.
Was This Post Helpful? 0

#15 TokoYami200   User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 44
  • Joined: 18-October 10

Re: Noob > need to make ascii triangle/pig latin program

Posted 01 February 2011 - 09:06 AM

The pig latin program actually seems hard, but it's pretty easy. Think about it in non-programming terms; if someone says "Hi, my name is Bob" and you want to convert that to pig latin you:

1. Seperate the sentence into words
2. Take every first letter, if it's a consonant move it to the end of the word with -ay (ob-Bay for Bob) if it's a vowel just add -way
3. Combine it into a full sentence

So in programming, it'll be practically the same method of tackling the problem. To split the input into words, you use the split() method. This basically splits up a string by whatever term.

String s = "This is a string.";
String[] Words = s.split(" "); //Splits it by spaces, ergo, it returns the words.



To continue, you loop through the array created by split and create a new sentence by moving the letter to the end and adding ay etc. Check out String's charAt() method and the StringBuilder class to continue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1